jQBrowser - Extend jQuery’s browser detection capabilities

jQBrowser is a plugin for the jQuery JavaScript library that replaces the $.browser object with a more robust and extensive detection based upon work by Peter-Paul Koch. This document describes jQBrowser version 0.2.


Author

Dave Cardwell (jQuery plugins)


Changelist

  1. v0.2 - Thursday 24th August, 2006

  2. v0.1 - Wednesday 16th August, 2006


Usage

The uncompressed source code is heavily documented. After reading this synopsis, that would be a good place to look.

Browser detection

You can either use $.browser.browser() to get the detected browser, or check for a specific browser yourself.

var browser = $.browser.browser(); // the detected browser (defaults to 'Unknown')

// The following functions return a boolean value indicating whether or not the given browser was detected:
var aol       = $.browser.aol();       // AOL Explorer
var camino    = $.browser.camino();    // Camino
var firefox   = $.browser.firefox();   // Firefox
var flock     = $.browser.flock();     // Flock
var icab      = $.browser.icab();      // iCab
var konqueror = $.browser.konqueror(); // Konqueror
var mozilla   = $.browser.mozilla();   // Mozilla
var msie      = $.browser.msie();      // Internet Explorer Win / Mac
var netscape  = $.browser.netscape();  // Netscape
var opera     = $.browser.opera();     // Opera
var safari    = $.browser.safari();    // Safari

Version detection

$.browser.version.string() returns the full browser version string detected, while $.browser.version.number() attempts to wrangle that string into a usable number.

alert( "You're using version " + $.browser.version.string() ); // defaults to 'Unknown'
if( $.browser.version.number() > 1.5 ) { // defaults to undefined
    // Do something...
}

Operating system detection

You can either use $.browser.OS() to get the detected operating system, or check for a specific OS yourself.

var os = $.browser.OS(); // the detected OS (defaults to 'Unknown')

// The following functions return a boolean value indicating whether or not the given operating system was detected:
var linux = $.browser.linux(); // Linux
var mac   = $.browser.mac();   // Mac OS
var win   = $.browser.win();   // Microsoft Windows

Notes

Warning: Browser detection is always an imprecise science. 99% of the time object detection is what you’re after.

Unlike the native jQuery $.browser object, the browser detection is accessed by functions so you need to use parantheses (ie. $.browser.msie() instead of $.browser.msie).

While the list of browsers and operating systems is not exhaustive, I used Wikipedia to determine the user agent strings of the most common. It is trivial to add new detection, so either modify the source for your needs or contact me if you think I’ve missed out a major browser that should be in the list.

Version detection does not work in Safari

To quote Peter-Paul Koch:

Unfortunately Safari’s string never contains its official version; only its internal Apple version number (ie. not 1.3.2 but 312.6). Therefore the version number detect doesn’t work in Safari. Since this is clearly Apple’s fault (it doesn’t follow established conventions), I don’t care.

Download

jQBrowser is dual-licensed under the MIT and GNU GPL licenses, so if you can use jQuery you can use this plugin.

If you find a use for jQBrowser I’d love to hear about it. Also, a link to this site wouldn’t go amiss!

I recommend you download the version that I’ve compressed with Dean Edwards’ packer which weighs in at 2.6K. Alternatively I make the heavily documented, uncompressed version available at 12.5K.


Bugs

jQBrowser v0.2 has no known bugs.


Testing