[JavaScript] navigator Object

The “navigator” property of the “window” object returns the navigator object, which contains the information about the browser. Getting browser information is quite important. In production environment, you are going to use the framework library such as Modernizr or browserhawk. But it never hurts to understand how you can do it yourself.


1. Properties

You can retrieve browser-specific information.

  • appCodeName : the internal code name of the browser
  • appName : the browser name
  • appVersion : the version
  • cookieEnabled : whether cookies are enabled
  • onLine : whether the browser is on line
  • platform : the platform for which the browser is compiled
  • userAgent : the user-agent header sent by the browser to the server
console.log(navigator.appName + ", " +
  navigator.appVersion + ", " + navigator.platform);
console.log(navigator.cookieEnabled + ", " + navigator.onLine);


2. Methods

You can check some of the browser capabilities using the following methods:

  • javaEnabled() : whether the browser has Java enabled
  • taintEnabled() : removed, do not use it
// alert(navigator.taintEnabled());


3. Browser Detection – IE

Internet Explorer is widely used but has some compatibility issues among different versions. Therefore it is quite common to detect which IE version is used.

  if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) {
    //test for MSIE x.x;
    var ieversion = new Number(RegExp.$1);
    if (ieversion >= 10)
      console.log("You're using IE10 or above");
    else if (ieversion >= 9)
      console.log("You're using IE9.x");
    else if (ieversion >= 8)
      console.log("You're using IE8.x");
    else if (ieversion >= 7)
      console.log("You're using IE7.x");
    else if (ieversion >= 6)
      console.log("You're using IE6.x");
    else if (ieversion >= 5)
      console.log("You're using IE5.x");

The “userAgent” values that are returned by IE are like these:

Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; …
Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; …
Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.0; Trident/5.0)

IE fakes it is like a “Mozilla” browser. But it also includes extra information. The regular expression will extract the version number.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s