spacer

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

home / web / tools / browser / javascript

JavaScript Browser Sniffer

Developer News
Metasploit 3.2 Offers More 'Evil Deeds'
'Thank You Apple. Seriously.'
The Buzz: BlackBerry App Store Seen Next
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.



JupiterOnlineMedia

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

Solutions
Whitepapers and eBooks
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
Avaya Article: Call Control XML - Powerful, Standards-Based Call Control
Tripwire Whitepaper: Seven Practical Steps to Mitigate Virtualization Security Risks
Internet.com eBook: The Pros and Cons of Outsourcing
Go Parallel Article: Scalable Parallelism with Intel(R) Threading Building Blocks
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
Go Parallel Article: James Reinders on the Intel Parallel Studio Beta Program
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
Go Parallel Article: Getting Started with TBB on Windows
HP eBook: Storage Networking , Part 1
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Go Parallel Video: Intel(R) Threading Building Blocks: A New Method for Threading in C++
HP Video: Is Your Data Center Ready for a Real World Disaster?
Microsoft Partner Portal Video: Microsoft Gold Certified Partners Build Successful Practices
HP On Demand Webcast: Virtualization in Action
Go Parallel Video: Performance and Threading Tools for Game Developers
Rackspace Hosting Center: Customer Videos
Intel vPro Developer Virtual Bootcamp
HP Disaster-Proof Solutions eSeminar
HP On Demand Webcast: Discover the Benefits of Virtualization
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Microsoft Download: Silverlight 2 Software Development Kit Beta 2
30-Day Trial: SPAMfighter Exchange Module
Red Gate Download: SQL Toolbelt
Iron Speed Designer Application Generator
Microsoft Download: Silverlight 2 Beta 2 Runtime
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
IBM IT Innovation Article: Green Servers Provide a Competitive Advantage
Microsoft Article: Expression Web 2 for PHP Developers--Simplify Your PHP Applications
Featured Algorithm: Intel Threading Building Blocks - parallel_reduce
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES
webref The latest from WebReference.com Browse >
Popular JavaScript Framework Libraries: An Overview · Controllers: Programming Application Logic - Part 2 · How to Use JavaScript to Validate Form Data
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Choosing the Right Online Backup Provider · Mother Avaya Nurtures Her Technology Partners · Software as a Service a Winning Model for Hotspot Provider

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

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