DekGenius.com
[ Team LiB ] Previous Section Next Section

5.4 Detecting the Netscape Navigator Version

NN 2 , IE n/a

5.4.1 Problem

You want script execution to branch based on a specific or minimum version of Netscape Navigator.

5.4.2 Solution

For versions prior to Netscape 6, the most easily accessed version information is at the start of the navigator.appVersion property value. Starting with Netscape 6, however, the version information is contained in a new property of the navigator object: vendorSub. Here's a function that returns a numeric value of the Navigator version from any generation:

function getNNVersionNumber( ) {
    if (navigator.appName =  = "Netscape") {
        var appVer = parseFloat(navigator.appVersion);
        if (appVer < 5) {
            return appVer;
        } else {
            if (typeof navigator.vendorSub != "undefined") {
                return parseFloat(navigator.vendorSub);
            }
        }
    }
    return 0;
}

You can use the returned value to establish a global variable that gets used elsewhere in code execution branch condition statements. For example, here's how to execute code only if the browser identifies itself as being compatible with NN 6 or later:

var isNN6Min = getNNVersionNumber( ) >= 6;
...
if (isNN6Min) {
    // perform statements for NN 6 or later
}

5.4.3 Discussion

As time goes on, there is a potentially increasing hazard: looking for any specific browser application name and version may shut out users whose browsers utilize the very same engines that your code accepts. All major browser makers, including Microsoft, Netscape (Mozilla), and Opera, have designed their code to be embedded as browser functionality in other types of devices. For example, if the same Gecko engine used in Netscape 6 and later should find its way into a future handheld device or consumer appliance, the browser will likely not bear the Netscape name or same versioning sequence. Of course, this particular engine includes other properties of the navigator object (product and productSub) that identify the Gecko engine and its build version. This is all the more reason that browser and version branching is less appealing than it once was, unless you use it to work around known bugs in existing versions.

5.4.4 See Also

Recipe 5.2 and Recipe 5.3 for additional browser detection tips; Recipe 2.1 for uses of the parseFloat( ) function.

    [ Team LiB ] Previous Section Next Section