| home / programming / javascript / designing / chap6 / 1 | [previous] [next] |
|
Now you begin to see the power behind these properties: they can determine the application name, version, and platform for any browser.
| Browser versions | |
You might notice something odd about the values of
The same situation occurs with Internet Explorer 5, 5.5, and 6, which all report an
|
|
The first of these, navigator.appName, is the most straightforward;
it tells you the name of the browser and nothing more. If you are using Netscape Navigator, the
value of appName is "Netscape". If you are using Internet Explorer, the value is
"Microsoft Internet Explorer".
The second property, navigator.appVersion, returns both the version
number and the browser's platform. According to Table 6-2, when you ask Netscape Navigator 4.0
for its appVersion, the browser returns "4.0 (Win95; I)". While this value makes sense,
many of the values listed for appVersion in Table 6-2 don't seem very intuitive. See the "Browser
versions" sidebar for details.
The third property, navigator.userAgent, essentially combines the first
two properties. The userAgent property is not new to the browser world; it was around long
before JavaScript. Many servers use the information gathered from userAgent to determine
which browsers are being used to access their sites. For the most part, however, we will use
appName and appVersion, because they contain all the information we need.
You can access the properties of the navigator object just as you would those of any
other object. For example, to print out the name of a browser, just pass navigator.appName to
document.write():
document.write("Welcome all " +
navigator.appName +
" users.");
There's not much reason to do this, however. The point of checking browser identities is to tailor your pages (or your entire site) for different browsers. The rest of this chapter is all about doing just that.
The browser's name and major version number (2, 3, 4, etc.) almost always determine which features a browser supports. For example, IE 3 does not support Microsoft's version of Dynamic HTML, but IE 4 and later versions do. Navigator 2 and IE 3 do not support JavaScript image replacement, but all later versions of both browsers do. If you know what browser and version you're dealing with, you know a lot about what kind of documents you can serve.
To determine the browser's name and version, we'll use two of the properties you just
learned about: appName and appVersion. To make our browser detection script
shorter, we start by assigning the values of these properties to two variables, as shown in Example 6-1.
var browserName = navigator.appName;
var browserVersion = parseInt(navigator.appVersion);
Example 6-1: Assigning the values of appName and appVersion to variable
First, the browser's name is assigned to the variable browserName. The
second line assigns the browser's version to the variable browserVersion. Note that this line
uses a built-in JavaScript function, parseInt(), to extract the integer value (whole
number) from navigator.appVersion. It works like this: the appVersion of
Netscape 6.01 on Windows Me is "5.0 (Windows; en-US)". When this is passed to parseInt(),
the returned value is simply 5, an integer. The appVersion of Internet Explorer 4.0 for
Windows 95 is "4.0 (compatible; MSIE 4.0b1; Windows 95)". When this is passed to parseInt(),
the returned value is 4.
| home / programming / javascript / designing / chap6 / 1 | [previous] [next] |
Created: December 12, 2001
Revised: December 12, 2001
URL: http://webreference.com/programming/javascript/designing/chap6/1/2.html