Using the navigator
object we can retrieve information about the capabilities and nature of the browser that is executing our code. If we'd like to execute different code for different browsers, this is the object for us.
Instead of describing what each of the properties do, it's easier to use some and demonstrate them by querying some of the recent release of Microsoft and Netscape browsers using this JavaScript code:
alert(navigator.appName);
alert(navigator.appVersion);
alert(navigator.appCodeName);
alert(navigator.userAgent);
alert(navigator.cookieEnabled);
The following table shows the values that IE4 returned:
Property | Value |
appName |
Microsoft Internet Explorer |
appVersion |
4.0 (compatible; MSIE 4.0; Windows NT) |
appCodeName |
Mozilla |
userAgent |
Mozilla/4.0 (compatible; MSIE 4.0; Windows NT) |
cookieEnabled |
True |
For comparison here's the Navigator 4.02 values:
Property | Value |
appName |
Netscape |
appVersion |
4.02 [en] (WinNT; I; Nav) |
appCodeName |
Mozilla |
userAgent |
Mozilla/4.02 [en] (WinNT; I; Nav) |
cookieEnabled |
undefined |
The values for Navigator 3.01:
Property | Value |
appName |
Netscape |
appVersion |
3.01 (WinNT; I) |
appCodeName |
Mozilla |
userAgent |
Mozilla/3.01 (WinNT; I) |
cookieEnabled |
True |
And also the values for Internet Explorer 3.01:
Property | Value |
appName |
Microsoft Internet Explorer |
appVersion |
2.0 (compatible; MSIE 3.01; Windows NT) |
appCodeName |
Mozilla |
userAgent |
Mozilla/2.0 (compatible; MSIE 3.01; Windows NT) |
cookieEnabled |
True |
The appName
and userAgent
properties (and their derivatives appVersion
and appCodeName
) reflect the browser that is executing the script code.
In case you haven't heard the explanation behind the Mozilla code name yet… it goes like this: Marc Andreesen who helped to develop the Mosaic browser while in college, once he left, founded Netscape, and started work on a browser that should 'beat its chest and smash Mosaic.' Mosaic + Godzilla = Mozilla. Get it?
There are two methods of the navigator
object.
Methods | Description |
javaEnabled |
Returns true or false depending on whether a Java VM is installed or not |
taintEnabled |
Returns false, included for compatibility with Netscape Navigator – the title seems to be Microsoft's little joke! |
There are two collections of the navigator
object as well.
Collections | Description |
plugins |
An alias for the collection of all the <EMBED> ded objects in the page |
mimeTypes |
Collection of all the document and file types supported by the browser |