Browser/OS detection with jQuery
As I wrote in my recent post I simply use the PPK BrowserDetect object to find out the browser/OS of the client. Actually I’m pretty sure there’s a similar jQuery plugin based again on that code from Quirksmode.org, but however I decided to write a simple plugin for jQuery wrapping it into a closure and exporting it in the “$” object.
Demo
You can see full working example here.
Installation
You must add a link to jquery.client.js after the jquery.js file in your code:
<html> <body> <div id="os"></div> <div id="browser"></div> <script src="./jquery-1.3.2.js"></script> <script src="./jquery.client.js"></script> <script> $('#os').html("<b>" + $.client.os + "</b>"); $('#browser').html("<b>" + $.client.browser + "</b>"); </script> </body> </html>
Now there’s a $.client object containing two strings with OS and browser, they can be referenced with:
$.client.browser $.client.os
You can append this code after you jquery.js installation. And if you’re on Firefox you simply can test with the console object:
Download
You can download the sample code from here.
Related posts:







This is very interesting. I think Jquery supported this functionality (jQuery.browser and jQuery.browser.version) but fro some reason it was deprecated in jQuery 1.3.
Since most of the time when i need this functionality is to determine the IE version (IE6 and IE7 are very different), does you plug-in also support version number?
Amir
Not the plugin does not support any version numbers. Actually $.browser.version is supporting such numbers. The $.client plugin is helping you mostly for OS detection, I guess.
Greetings,
stoimen
You can easily include the browser version functionality by adding this line in the “jquery.client.js” file (probably located at the last of the script file).
replace this line
window.$.client = { os : BrowserDetect.OS, browser : BrowserDetect.browser};
with this
window.$.client = { os : BrowserDetect.OS, browser : BrowserDetect.browser, browserversion : BrowserDetect.version };
and you will also get the browserversion.
Yeah, thanks I’ll add this soon!
Thank you guys this is exactly what i need.
Hi,
Thank you for this plugin. I have written a little introduction on my blog for your plugin.
Hi Mengu,
I’m glad I could help you! Thank you for your post, I think it’s great and I’ll definitely start following your blog.
greetings,
stoimen
Hi Stoimen,
Thank you for your comment. I’m glad at least someone liked it.
Hello, I would like to use your code in a wordpress plugin. Under what license is your code available? Just to be proper… Also when finished, I will include your links and licenses in the sources and the readme. Keep up the coding and sharing! ;]
Hi,
you’re free to use the code with no limitations, it’s completely open/free source. I’d love to see a reference in your plugin’s readme file it would be very nice.
cool dude
very nice i appreciate!
Hi,
Would be nice if you add support for iPad and other mobile devices as well.
Regards,
Kumar Amit Ranjan
Yeah! Regarding of how new is the iPad I’ve to update a bit the source code!
Thanks
Awesome plugin, thanks so much! I have this one little question, though… how do I make it conditional? Say, if the user is on Windows, they will see one thing, if on a Mac, they see another. I need this for a software download link.
@Ragnar – well you can simply compare it like:
The possible strings to compare with OS are:
Windows Mac iPhone/iPod LinuxFor browsers:
Hi,
nice idea – it seems to work under some circumstances and somehow not in my Opera under Windows 7 what is to bad.
About Browser-Versions I want to let you know that this will probably not work for Safari because Apple has only a kind of internal Versioning and not common version numbers – at least that’s what I read recently.
Well, I’ve to check this out! Thanks
Awesome plug-in! I was on the verge of writing an encapsulation for the $.browser property in jQuery but this solution is much cleaner. Thank you!
Thank you very much for providing a complete solution!
Great and simple plugin.
I am using in a website i am doing and i will put a note in the script header about you.
Thanks!
THANK YOU VERY MUCH! Great work!
Thanks dude, this is so nice and simple. It also compresses to 1.24KB
http://closure-compiler.appspot.com/code/jsc1a1f42dd415b9fe11d484ca9f9591418/default.js
I see you added code to detect browser version. do you have code to detect OS version as well. I need to detect os versions of mobile devices. some os versions of android do not support Typekit (Android 2.2 and higher only) and I need account for this by detecting Android 2.1
can the script be updated to detect Android os and browser versions?
How could I use this to load a different style sheet?
@Tyler,
I’ve seen a solution around I think you could use here as well. In short, you add to the html tag different CSS classes depending on $.client.browser and/or $.client.os and so on.
then you’ll need a CSS with those classes with the right properties for specific browser(s) you want. For instance:
.Safari {…} /* CSS for Safari */
.Explorer {…} /* CSS for MSIE */
And if also body.addClass($.client.os); you need more than just one class in CSS:
.Safari.Mac {…}
.Safari.Win {…}
Hope this helps.
Nice work.
I’m creating an application which detects whether the OS is 32 or 64bit architecture and then offers up an installer file. The main app is a pyside qt application which can handle all the OS checking for architecture but I needed to create a browser compatible fall back. This library helped lots but I decided to mod it slightly to do some basic architecture checks.
In the init: function part I added a call:
added a new data object under dataOS:
and finally added it to the window.client call:
Hope it might be useful to someone
Si
Great job! This is incredibly useful to me. I was having trouble with differences in UL spacing between Mac and Windows and this will allow me to use two different style sheets to compensate.
cheers,
aaron
Thank very much ‘ for this script. Great work!
Can you tell me if I can detect OS version with this plugin, like Windows 7 and Windows XP ?
@Pranjal – no, you can’t.