Netscape 6, Part VI: Object-Oriented DOCJSLIB 1.2: Browser Sniffer - Doc JavaScript | WebReference

Netscape 6, Part VI: Object-Oriented DOCJSLIB 1.2: Browser Sniffer - Doc JavaScript


Netscape 6, Part VI: Object-Oriented DOCJSLIB 1.2

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

https://www.internet.com


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

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