1. Technology

The Browser Object Model

9. navigator


The window.navigator object is one of the oldest parts of JavaScript that has never been standardised. It dates back to the original JavaScript implementation in Netscape Navigator 2 and has been sort of copied into every JavaScript implementation in every browser that has come out since.

Unfortunately, while each browser implements some of the same properties for this object, what they put in the properties can vary significantly between browsers making this object close to useless. There are also big differences between browsers as to what properties that they do implement.

Perhaps the most useful properties are navigator.language and navigator.systemLanguage since the first two characters of one of these two fields is usually populated with a two character code representing which language the operating system on your visitor's computer is set to use. In some but not all cases this is followed by a second two character code identifying the country variant of the language but some browsers only supply two characters. The value may also be in upper or lower case and so we need to do a little work on these fields to retrieve the two character language code in a consistent format.

var lang = (navigator.language || navigator.systemLanguage).toLowerCase().substr(0,2);

Properties which all browsers do implement include navigator.userAgent, navigator.appCodeName, navigator.appName, and navigator.appVersion. The last three of these are extracted from the first and so all four of these are as meaningless as each other. The reason why they are meaningless is that they contain information that is supposed to identify which browser that it is and what operating system that it is running on. The problem is that the information in these fields is not necessarily correct for a number of reasons.

  • The useragent data is able to be changed to anything at all by the browser owner in some browsers such as Internet Explorer and Firefox. These browsers allow the browser owner to set these fields to contain any value at all of their own choice.
  • Because a lot of people misused these fields to detect which browser was being used so as to provide browser specific processing many browsers misidentify themselves deliberately as a different browser to bypass those meaningless tests. For example both Safari and Opera have provision for the useragent identifying them as Internet Explorer or Firefox
  • Some browsers can automatically change the value in the useragent to match that which works best for different web sites.
  • Browsers do not know about operating systems released after the browser and so the operating system version information is even less accurate than the inaccurate browser information.

Testing the userAgent or any of the three fields extracted from it is useless for anything except statistical purposes (where you at least have a reasonable percentage of the values that will be reporting correctly). Since capturing and saving the statistical information requires server side processing, referencing the values from JavaScript is completely pointless.

Most browsers provide a few other properties for the navigator object beyond those already mentioned but since they all tend to be specific to one browser or at least have limited support, and since other browsers do not provide an alternative way to obtain the same information, use of any of the other properties is completely pointless since most of your visitors will not have a browser that supports it.

More of this Tutorial

Related Video
Screen Capture With the Snip Tool in Windows Vista
  1. About.com
  2. Technology
  3. JavaScript

©2014 About.com. All rights reserved.