Ultimate JavaScript Client Sniffer

See NetScape for original document

Version Number

Browser Version

JavaScript Version

OS

 


SO WHY DIDN'T WE MAKE THIS CODE OBJECT ORIENTED?

ie. like this...
 

function BrowserCheck () {

   var agt=navigator.userAgent.toLowerCase();
   this.major = parseInt(navigator.appVersion);
   this.minor = parseFloat(navigator.appVersion);

   this.nav  = ((agt.indexOf('mozilla')!=-1) && ((agt.indexOf('spoofer')==-1)
                && (agt.indexOf('compatible') == -1)));
   this.nav2 = (this.nav && (this.major == 2));
   this.nav3 = (this.nav && (this.major == 3));

   ...and so on ...
   this.vms   = (agt.indexOf("vax")!=-1) || (agt.indexOf("openvms")!=-1);
}

var is = new BrowserCheck(); 

if (is.nav) {  ... navigator code here ... }
else if (is.ie) {  ... explorer code here ... }
The answer is simple:

this code is far more elegant, providing encapsulation of the variables with an enclosing "is" object, but it breaks on Internet Explorer 3 for the Macintosh. If you create an "is" object on IE3 for the Mac, the first time the page is loaded, the code will work fine, however any reloads of the page will cause the browser to crash. To get around this on IE3 for the Mac, we don't create an "is" object; instead, we create bunch of boolean variables which have similar names. This is ugly, but it's the price we pay for working around this bug of JScript for the Macintosh in IE3.

Another possible workaround is to use the object oriented code on all other browsers but wrap it in a check which avoids executing the object oriented code on IE3 for the Mac. This preserves the object oriented design of the code but requires an extra boolean check like if (!isIE3Mac &&is_nav4up) each time you reference the is object. As this extra boolean check is inconvenient, we've resigned ourselves to the simple non-object oriented version above. However, if you prefer the object oriented approach, we provide a object oriented client sniffer with a safety check for the Mac. Finally, if you don't need to support IE3 for the Mac, you can use that object oriented code and omit the Mac safety check.
 

1
Hosted by www.Geocities.ws