spacer

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

home / web / tools / browser / javascript

JavaScript Browser Sniffer

Developer News
Mandrake Linux Founder Back, Virtually
Amazon: We're a Technology Company
Sun Expands MySQL With Closed Source
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.

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Whitepapers and eBooks

Intel Whitepaper: Comparing Two- and Four-Socket Platforms for Server Virtualization
IBM Solutions Brief: Go Green With IBM System xTM And Intel
HP eBook: Simplifying SQL Server Management
IBM Contest: Are You the Next Superstar? Join the "Search for the XML Superstar" Contest to Find Out
Microsoft PDF: Top 10 Reasons to Move to Server Virtualization with Hyper-V
Microsoft PDF: Six Reasons Why Microsoft's Hyper-V Will Overtake Vmware
Microsoft Step-by-Step Guide: Hyper-V and Failover Clustering
Intel PDF: Quad-Core Impacts More Than the Data Center
Intel PDF: Virtualization Delivers Data Center Efficiency
Go Parallel Article: PDC 2008 in Review
Microsoft PDF: Top 11 Reasons to Upgrade to Windows Server 2008
Avaya Article: Communication-Enabled Mashups: Empowering Both Business Owners and IT
Intel Whitepaper: Building a Real-World Model to Assess Virtualization Platforms
  PDF: Intel Centrino Duo Processor Technology with Intel Core2 Duo Processor
Microsoft Article: Build and Run Virtual Machines with Hyper-V Server 2008
Go Parallel Article: Q&A with a TBB Junkie
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
IBM eBook: The Pros and Cons of Outsourcing
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
HP eBook: Guide to Storage Networking
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
webref The latest from WebReference.com Browse >
Popular JavaScript Framework Libraries: An Overview - Part 3 · Accessing Your MySQL Database from the Web with PHP · Working with the DOM Stylesheets Collection
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Crucial Triples Up With New Three-Channel DDR3 Kits · Meet the Finalists: Excellence in Technology Awards · Tealeaf Offers Insight to Mobile Customer Behavior

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

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