spacer

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

home / experts / javascript / column77


Netscape 6, Part VI: Object-Oriented DOCJSLIB 1.2

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

Browser Sniffer

The file browserSniffer.js includes the function browserSniffer() which creates a new object, detects the browser type, and assigns a true/false property for each browser.

function browserSniffer() {
  this.ie4 = document.all && !document.getElementById;
  this.ns4 = document.layers;
  this.ie5 = document.all && document.getElementById;
  this.ns6 = document.getElementById && !document.all;
}

This is a very basic browser sniffer. It relies on the some differentiators between the browsers. Netscape Navigator 4.x is the only one supporting document.layers. Both Internet Explorer 4.x and Internet Explorer 5.x support the object document.all. Internet Explorer 5.x differs from its predecessor by adhering to the DOM standard and supporting the method document.getElementById(). Netscape 6 adheres to this standard as well and therefore supports this method as well, but the browser does not support document.all.

Your application may need a finer browser sniffer. You may have functions that are different between Windows and Macintosh. Add more properties to the above object and name them as mnemonic as possible. For example, use the name ie4mac to name the property that holds the browser type of Internet Explorer 4.x on Mac. To detect the operating system, you can use the following lines:

var browser = navigator.userAgent.toLowerCase();
this.win = browser.indexOf("win")!=-1;
this.mac = browser.indexOf("mac")!=-1;

Next: How to create a browser-dependent subclass on the fly

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


Produced by Yehuda Shiran and Tomer Shiran
All Rights Reserved. Legal Notices.
Created: February 12, 2001
Revised: February 12, 2001

URL: http://www.webreference.com/js/column77/5.html