DekGenius.com
[ Team LiB ] Previous Section Next Section

5.2 Detecting an Early Browser Version

NN 2, IE 3

5.2.1 Problem

You want script execution to branch based on an early version of the browser.

5.2.2 Solution

For mainstream browsers through Version 4, the navigator.appVersion property returns the complete version of the browser along with further information about the operating system platform. To determine the major generation of the browser, use the parseInt( ) function to extract the integer value of the version:

var isVer4 = (parseInt(navigator.appVersion) =  = 4);

To get the version number that includes incremental upgrades of the browser (represented as numbers to the right of the decimal, such as 4.74), use the parseFloat( ) function:

var isVer4_74 = (parseFloat(navigator.appVersion) =  = 4.74);

If you want your variable to indicate that a certain minimum version is available, use the >= operator instead of the equality (= =) operator:

var isVer4Min  = (parseInt(navigator.appVersion) >= 4);
var isVer4_5Min = (parseFloat(navigator.appVersion) >= 4.5);

5.2.3 Discussion

For any browser version up to Microsoft Internet Explorer 4.0 and the last released version of the Netscape Navigator 4 family (4.79 as of this writing), the navigator.appVersion string begins with accurate information about the browser's version. For example:

Microsoft Internet Explorer 4.01 running on Windows 98
    4.0 (compatible; MSIE 4.01; Windows 98)
Netscape Navigator 4.79 for the Macintosh PowerPC
    4.79 (Macintosh; U; PPC)

But for all subsequent browser versions, the leading characters of the navigator.appVersion string no longer correspond to the browser's actual version number. Instead, the leading number represents the generation of core browser code. Thus, the lead part of the navigator.appVersion string, even for Internet Explorer 6 for Windows, continues to read 4.0.

Newer Netscape browsers are built with an entirely new core engine (the Gecko engine developed by Mozilla). For a variety of historical reasons, this generation of code is counted as the fifth generation. Therefore, because the fifth-generation core engine is used in the Netscape 6 and 7 browsers (and Mozilla releases), the navigator.appVersion string for those browsers begins with 5.0, rather than a number corresponding to the actual browser version.

To determine the precise browser version number for newer browsers, you must use other techniques which are, unfortunately, not cross-browser compatible. These are described in Recipe 5.3 and Recipe 5.4.

5.2.4 See Also

Recipe 5.1, Recipe 5.3, and Recipe 5.4 for additional browser detection tips.

    [ Team LiB ] Previous Section Next Section