spacer

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

home / experts / javascript / column3


Fourth Generation Browser Detection

Developer News
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?

Our script utilizes JavaScript 1.2, first supported by Navigator 4.0x and Internet Explorer 4.0x, so we must make sure it does not affect users with older browsers.

There are two ways to determine which browser the user is running:

  • You can evaluate navigator.appVersion or navigator.userAgent.
  • You can check if the browser supports specific objects (or properties) that the script requires.

The following script creates two Boolean variables that indicate whether the user is running Navigator 4.0x (or above) or Internet Explorer 4.0x (or above):

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

var bName = navigator.appName;
var bVer = parseInt(navigator.appVersion);
var NS4 = (bName == "Netscape" && bVer >= 4);
var IE4 = (bName == "Microsoft Internet Explorer" && bVer >= 4);

// -->
</SCRIPT>

The following script does the exact same by checking if the browser supports specific objects:

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

var NS4 = (document.layers) ? 1 : 0;
var IE4 = (document.all) ? 1 : 0;

// -->
</SCRIPT>

NS4 is true if the browser supports the document.layers object. In other words, it is true for Navigator 4.0x. The second variable, IE4, is true if the browser supports the document.all object. That is, it is true for Internet Explorer 4.0x. Note that 1 and 0 are equivalent to true and false. We use them because they are shorter.

We'll use the second detection method in our scripts. By evaluating NS4 and IE4 we can find out if the user is running a fourth-generation browser:

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

var NS4 = (document.layers) ? 1 : 0;
var IE4 = (document.all) ? 1 : 0;
if (IE4 || NS4)
  document.write("This is a fourth-generation browser!");

// -->
</SCRIPT>

This script simply prints "This is a fourth-generation browser!" if the user is running Navigator 4.0x+ or Internet Explorer 4.0x+.

http://www.internet.com

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

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

webref The latest from WebReference.com Browse >
Rolling Out Your Own HTML Application Version Control · HTML 5: Client-side Storage · Working with Ajax Server Extensions
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Wi-Fi Product Watch, November 2009 · Chip Market Recovering From '08 Collapse · Low-Cost Tools to Kickstart Your New Business


Created: September 25, 1997
Revised: April 16, 1998

URL: http://www.webreference.com/js/column3/detection.html