Now you begin to see the power behind these properties: they can determine the application name, version, and platform for any browser.
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
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
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
userAgent to determine
which browsers are being used to access their sites. For the most part, however, we will use
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
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.
Browser name and number
To determine the browser's name and version, we'll use two of the properties you just
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
second line assigns the browser's version to the variable
browserVersion. Note that this line
parseInt(), to extract the integer value (whole
navigator.appVersion. It works like this: the
Netscape 6.01 on Windows Me is "5.0 (Windows; en-US)". When this is passed to
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
the returned value is 4.
Created: December 12, 2001
Revised: December 12, 2001