"Browser sniffing" is scanning the userAgent property of the navigator object. The userAgent property is a text string that can be "scanned" with the indexOf() method of a String object variable. The indexOf() method can be applied directly to the userAgent property with the form:
navigator.userAgent.indexOf(<string>)
but, for the sake of designing efficiently, navigator.userAgent is usually set to a variable. A more elaborate but most compartmentalized design is to sniff for browsers with a class function. The following illustrates:
function jsAgent() {
vAgent = window.navigator.userAgent
this.ms3x95 = (vAgent.indexOf("MSIE 3.02") != -1 &&
vAgent.indexOf("Windows 95") != -1) ? true:false
this.ms3xNT = (vAgent.indexOf("MSIE 3.02") != -1 &&
vAgent.indexOf("Windows NT") != -1) ? true:false
this.ieMac = (vAgent.indexOf("Mac") != -1 &&
vAgent.indexOf("MSIE") != -1) ? true:false
this.ieold = (vAgent.indexOf("Mozilla/1") != -1 &&
vAgent.indexOf("MSIE") != -1) ? true:false
this.ieX = (vAgent.indexOf("MSIE") != -1 &&
vAgent.indexOf("Windows NT") == -1 &&
vAgent.indexOf("Windows 95") == -1 &&
vAgent.indexOf("Mac") == -1) ? true:false
this.moz3or4 = (vAgent.indexOf("Mozilla/3") != -1 ||
vAgent.indexOf("Mozilla/4") != -1 ||
vAgent.indexOf("Mozilla/5") != -1) ? true:false
this.mozold = (vAgent.indexOf("Mozilla/2") != -1 &&
vAgent.indexOf("MSIE") == -1) ? true:false
this.agentX = (vAgent.indexOf("Mozilla") == -1 &&
vAgent.indexOf("MSIE") == -1) ? true:false
}
This "class function," jsAgent(), is looking for two types of browsers: Microsoft Browsers and Mozilla-Standard browsers. Because of Microsoft's historical behavior, the assumption here is that their early browsers may not contain the word "Mozilla" while Netscape browsers (being very intimate with mozilla.org) can be found by looking for "Mozilla."
The sniffing takes place when jsAgent() is instantiated. Suppose a variable called objAgent is used to instantiate jsAgent(), we then have:
objAgent = new jsAgent()
Using objAgent, I will make a few remarks. Suppose the following:
objAgent.ieX == true
This means that we have found a Microsoft Internet Explorer browser on an operating system that is not Windows- or Macintosh-based. For more details on dealing with sniffing for other operating systems (and more), please search for the MS Word .DOC file "Sniffing for Browsers, Java Virtual Machines, and Operating Systems" by Michael Edwards (March 1998) at microsoft.com.
Also suppose that:
objAgent.agentX == true
This means that we have a browser that is not from Microsoft nor Mozilla-based. This is truly a strange-smelling browser!
Now suppose that:
objAgent.moz3or4 == true
This means that we have found a browser that can handle CSS1 and HTML 4.0 Transitional without too many problems. As of this writing, such a browser is the best one can reasonably hope for at this proprietary moment in time.