Browser detection using jQuery
Browser sniffing is messy. There are a million ways to do it but none of them are particularly clean and most involve conditional statements such as “” for IE and various other CSS selector hacks for other browsers.
It get around this common issue i am now using jQuery to add a class to my body tag.
1 2 3 4 5 | if($.browser.msie){ $('body').addClass('browserIE'); // Add the version number $('body').addClass('browserIE' + $.browser.version.substring(0,1)); } |
If the user is using IE it will add a class to teh body tag like so:
1 | <body class="browserIE browserIE#"> |
The “#” stands for the version of IE the user is running.
You can now access elements on your page that neeed to be corrected for IE by calling:
1 2 3 | .browserIE6 #myDiv { display:block; } |
This will work much better then current hacks. Hope that helps someone!!


