WebReference.com - Part 1 of Chapter 6 from Designing with JavaScript (3/4) | WebReference

WebReference.com - Part 1 of Chapter 6 from Designing with JavaScript (3/4)

To page 1To page 2current pageTo page 4
[previous] [next]

Designing with JavaScript, 2nd Edition

Matching up names and numbers

Now that we have the browser name and its major version number, we need to match up the names and numbers. This is done with if statements. Example 6-2 shows an if statement that detects the presence of Netscape 6 using the browserName and browserVersion variables defined in Example 6-1.

if (browserName == "Netscape" && browserVersion == 5) {
    document.write("This is Netscape 6");
}
else {
    document.write("This is not Netscape 6");
}

Example 6-2: Checking for Netscape 6 and printing the results

This if statement compares both the browser's name and version: if the browser name is "Netscape" and the version is 5, it must be Netscape 6, so the code within the curly braces after the if is run, printing "This is Netscape 6" to the page. If the browser is not Netscape 6, the code in the curly braces following the else is run, printing "This is not Netscape 6".

  By combining a series of if statements, we can detect any browser that supports JavaScript.

By combining a series of if statements similar to the one in Example 6-2, we can detect any browser that supports JavaScript. Example 6-3 shows how to use if statements to piece together the identity of the browser. This script detects the latest two versions of Navigator and the latest four versions of Internet Explorer.

var browser;
if (browserName == "Netscape" && browserVersion == 5) {
    browser = "nn6";                                         // Netscape 6
}
else if (browserName == "Netscape" && browserVersion == 4) {
    browser = "nn4";                                         // Navigator 4
}
else if (browserName == "Microsoft Internet Explorer" && 
         browserVersion == 4 && 
         navigator.appVersion.indexOf("MSIE 6.0") != -1) {
    browser = "ie6";                                         // IE 6.0
}
else if (browserName == "Microsoft Internet Explorer" && 
         browserVersion == 4 && 
         navigator.appVersion.indexOf("MSIE 5.5") != -1) {
    browser = "ie55";                                        // IE 5.5
}
else if (browserName == "Microsoft Internet Explorer" && 
         browserVersion == 4 && 
         navigator.appVersion.indexOf("MSIE 5.0") != -1) {
    browser = "ie5";                                         // IE 5.0
}
else if (browserName == "Microsoft Internet Explorer" 
         && browserVersion == 4) {
    browser = "ie4";                                         // IE 4
}

Example 6-3: Detecting recent browsers from Netscape and Microsoft

  Detecting the different versions of Internet Explorer is complicated, because IE 4, IE 5, IE 5.5, and IE 6 all report a browser version of 4.

Based on the browser's name and version, this script sets the variable browser to one of six strings: "nn6", "nn4", "ie6", "ie55", "ie5", or "ie4", corresponding to Netscape Navigator 6 and 4 and Internet Explorer 6, 5.5, 5, and 4, respectively. Detecting the different versions of IE is more complicated than detecting different versions of Navigator, because IE 4, IE 5, IE 5.5, and IE 6 all report a browser version of 4. Thus, we have to use indexOf() to detect the "MSIE #.#" portion of appVersion in order to distinguish among these browsers.


To page 1To page 2current pageTo page 4
[previous] [next]

Created: December 12, 2001
Revised: December 12, 2001


URL: http://webreference.com/programming/javascript/designing/chap6/1/3.html