JavaScript Browser Sniffer: WebTools | WebReference

JavaScript Browser Sniffer: WebTools

JavaScript Browser Sniffer

Welcome to the WebReference.com browser sniffer. This JavaScript-enabled page sniffs out common browser environment variables useful for writing cross-browser scripts. Let's face it, writing cross-browser code is tricky enough, we're here to make your life a little easier. You can use the results and true/false tests below in your own scripts to test for capabilities/object support before you use that technique, to avoid errors and gracefully degrade. Look for links to actual examples of the test in action. If you've got a test/variable you'd like to see in here, don't hesitate to email us.

This sniffer is an updated combination of Erik Krock's Ultimate JavaScript Client Sniffer and Andy King's JavaScript sniffer, which features object detection useful for more robust JavaScripting. (Note: our use of the term "sniffer" here should in no way be confused with products from Sniffer Technologies, Inc.)

Updated: 12/5/2002. Added the is_Flash and is_FlashVersion variables. Our check is imperfect; so for now we look only for a positively identified version of the Flash Player. On one of our Windows test machines, for example, we were able to properly identify the Flash Player in IE4 but were unable to do so in IE5. Also, Mac Internet Explorer versions prior to version 5 will not work properly with this check; and, if the user has VBScript disabled, all IE/Win checks will fail. Therefore, because of these many anomalies, if we don't get a positive Flash Player ID, then we simply report "Can't Tell" to the user.

Updated: 1/10/2003. Added Safari sniffing. For now, we're sniffing only for the browser itself and not version; when we've gotten more information regarding version sniffing we'll add it. Also, like Opera (described below) we'll attempt to identify safari as such, and not gecko (even though Gecko and Netscape are present in the UA).

Updated: 1/28/2003. Added Konqueror sniffing, including an attempt to identify the actual version number from the user agent.

Updated: 6/17/2003 This update includes some bug fixes, a couple of changes to make the code more readable, and some script to detect Windows Media Player's presence and version. Contributed by Ben Nelson.

Updated: 11/24/2003. Added Firebird sniffing (is_fb, is_fb_ver). Similiar to the Mozilla sniff, except that we look specifically for Firebird in the navigator.vendor.

Updated: 03/25/2004. Added Firefox sniffing.

Updated: 09/19/2006. Belated additions for is_opera8, is_opera9, and ie_ie7 (and related sniffing). Contributed by Jonathan Warrington.

Updated: 10/09/2006. Added is_macosx, and also added safari to the list of browsers allowed into the Flash detection code. Contributed by Michael Hopkins.

Note that our detection method here attempts to identify Opera regardless of its masquerade setting (i.e., regardless of whether the user has Opera set to identify itself as "Opera," "MSIE," or "Mozilla/Netscape").



Navigator Object Data

Version Number

Browser Version

JavaScript Version

OS

Object Detection Tests

Method Detection Tests

Screen Properties

Document Properties

Flash Detection

Further Reading

Addendum

*Note: The document.getElementById can be used to test W3C DOM capability but should be used with caution. Some browsers give a "true" to this test, yet don't fully support the DOM (Opera, IE5.5). Make sure you use this test in combination with IE5.5, Opera, etc. platforms tests, and of course, test your code on the platforms you explicitly support.

**Note: Old DHTML scripts can fail in NS6 and above with the document.layers test, as NS6 does not support layers, but the W3C standard DOM1 and some of DOM2. Gecko m7 does not support the document.layers test. Use document.getElementById instead (note: ie5+ supports document.getElementById, so you'll need to allow for this in your code). Note also that Netscape 6 supports IE's innerHTML property for convenience, but it is not in the W3C DOM. So to make your DHTML scripts work in DOM-compliant browsers use DIVs, example:

<DIV id='MYDIV' style="position:absolute; left:200; top:100; visibility:hidden;'>
myElement = document.getElementById('MYDIV');
myElement.style.visibility = 'visible';
myElement.style.left = x + "px"; // note ie uses pixelLeft, NS4 uses moveTo(x,y)
myElement.style.top  = y + "px"; // note ie uses pixelTop
myElement.style.innerHTML = newcontent; // not in W3C dom but in NS6+
Example browser branching logic:
if (document.all) { // note, this code would also run in IE5+, which has partial support
  IE4+ code         // for the W3C DOM. You could also add check for !DOM 
                    // here (&& !(document.getElementById)) and equate dissimilar IE/W3C
                    // DOMs with prototype function, and run all IE5+/NS6+ browsers with
                    // only DOM-standards-compliant code. IE5+ browsers would then drop 
                    // down to third elseif below. 
} else if (document.layers) {
  NS4+ code 
} else if (document.getElementById) {
  NS6+ code         // and IE5+ code you flatten differences between MS DOM and W3C DOM
} 

Note that it would be interesting to see if someone could use the prototype function in JavaScript (see siteExperts.com) to equate document.all with document.getElementByID (or even better vice versa) and style.pixelLeft with style.left and style.pixelTop with style.top for cleaner code.

Created: Feb. 15, 1999
Revised: January 28, 2003

URL: https://www.webreference.com/tools/browser/javascript.html