spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / experts / javascript / column6


Browser Detection

Developer News
OpenOffice 3.2 Lands Amid Critical Changes
Red Hat, IBM Firmly in KVM Virtualization Camp
Red Hat Talks Up Open Source Cloud Plans

JavaScript features several properties that reflect the user's browser and platform specifications. These properties belong to the navigator object, which is supported by all JavaScript-enabled browsers.

Take a look at the following script:

<SCRIPT LANGUAGE="JavaScript">
<!--

var bName = navigator.appName;
var bVer = parseFloat(navigator.appVersion);
if (bName == "Netscape")
  var browser = "Netscape Navigator"
else
  var browser = bName;
document.write("You are currently using ", browser, " ", bVer, ".");

// -->
</SCRIPT>

This script prints the name of the browser and its version (as an integer). For example, it might print:

You are currently using Netscape Navigator 4.

The property navigator.appName is a string that reflects the name of the user's browser, such as "Netscape" or "Microsoft Internet Explorer".

The property navigator.appVersion reflects the browser's version. However, it is a string because it contains more than just the version number. For example, the value of this property might be "4.03 [en] (Win95; I)". Use the parseFloat() function to convert this property into a floating-point number that represents the version number only (e.g., 4.03).

Take a look at the following functions:

function bName() {
  // return 1 for Internet Explorer
  if (navigator.appName == "Microsoft Internet Explorer")
    return 1;

  // return 2 for Navigator
  if (navigator.appName == "Netscape")
    return 2;

  // return 0 for other browsers
  return 0;
}

function bVer() {
  // return version number (e.g., 4.03)
  return parseFloat(navigator.appVersion)
}

You can use these functions to execute a script segment only if the user is running Microsoft Internet Explorer 4.0+ or Netscape Navigator 4.0+:

var nameCode = bName();
var versionCode = bVer();
if ((nameCode != 0) && (versionCode >= 4)) {
  // additional statements here
}

http://www.internet.com


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

webref The latest from WebReference.com Browse >
Search Engine Optimization: Selecting and Embedding Keywords · Are Google's Language Translation Web Services Ready for Prime Time? · Installing and Using Meeplace, the Business Review CMS
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
IBM DB2 10 for z/OS: Justifying the Upgrade · Living La Vida Colo: Choosing the Right Colocation Facility · FTC Concerns over Social Media Privacy Linger

Created: November 4, 1997
Revised: December 4, 1997
URL: http://www.webreference.com/js/column6/browser.html