CSS Browser detection using jQuery instead of hacks

2 Dec 2008 | Jon Hobbs-Smith
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 "<!--[if condition]> HTML <![endif]-->" for IE and various other CSS selector hacks for other browsers.

I've done a fair amount of browser sniffing with jQuery recently and it's really easy, useful for when you need to detect the browser and version number in your javascript. It occurred to me that it would be easy to detect the browser and then put something in the DOM that your CSS could use for conditional formatting. So I wrote a quick script in JavaScript/jQuery.

How it works:


All you have to do is include the JavaScript file in the head of the page and it'll attach 2 classes to your body tag to say what browser and what version is being used so you've got 2 levels of granularity. Possible values are...

.browserIE
.browserIE6
.browserIE7
.browserIE8
.browserChrome
.browserChrome1
.browserSafari
.browserSafari1
.browserSafari2
.browserSafari3
.browserMozilla
.browserFirefox
.browserFirefox1
.browserFirefox2
.browserFirefox3
.browserOpera

If you need to detect any other browsers then it would be easy to modify the script.

All that happens automatically.

Usage example:


All you have to do is write your CSS selectors to use those classes where necessary, so let's say for example that you had an input textbox which looks correct in every browser except safari (quite common in my experience) and you want to position it 2 pixels lower in safari, all you have to do is set up the following 2 styles.

#myInput {background:black;} .browserSafari #myInput{background:black; position:relative; top:2px;}
Pretty neat compared to other solutions. The script is < 1k minified and the browser detection only needs to be done once on page load, from that point on it's CSS selectors all the way.


Get the code:


download files  browserDetect.js (2k)
download files  browserDetect-min.js (0.9k)


comments

Shadowfiend @ 4 Dec 2008 5:11 AM

I would argue that conditional comments are not a hack for the web developer. They're a hack in IE, yes, but that hack is in fact a useful feature for the developer.

Arguably detecting the browser using Javascript instead of using the built-in IE feature is, in fact, an even better plan, for a variety of reasons:

* You don't load the rules that won't be applying because they're not for this browser.

* You don't depend on a technology that can be turned off independently of CSS.

* You don't depend on a technology where a syntax error could temporarily break your browser detection.

* There's no sense in wasting browser time setting the relevant class and recalculating the styles everywhere. Yes, this time is essentially unnoticeable, but from a 'dirty vs clean' stand point I think that make it dirtier.

In short, I still think conditional comments are the way to go for IE targeting. Though this could come in useful in targeting styles for browsers other than IE, in my experience it's relatively rare to have to target styles that way, and when you do, you can typically work around it by doing things a little differently rather than applying a style to just one browser. Neat technique, of course, I just personally don't think it trumps existing ones for cleanliness.

Jon @ 4 Dec 2008 11:37 AM

I see your point and I think it comes down to personal preference.

Most of our sites rely on javascript being switched on to use all of their features anyway and personally I'm not a fan of the extra markup cause by traditional methods, or keeping a totally seperate stylesheet for IE which makes it hard to just go in and edit things in a single location.

David Walsh @ 5 Dec 2008 9:58 PM

Very cool!

Andy Ford @ 5 Dec 2008 10:14 PM

Well done. Aside from its dependency on jQuery, are there any significant differences to CSS Browser Selector? http://rafael.adm.br/css_browser_selector/

Just curious. I've yet to use either script but am certain I will soon

Jon @ 5 Dec 2008 10:51 PM

@Andy - Nope, they look very similar. This was just an idea I came up with on a recent project. That guy's clearly had the same idea :)

Karl Swedberg @ 5 Dec 2008 11:55 PM

Although I'm not a big fan of browser sniffing, I can see how this sort of thing might come in handy from time to time. I'd suggest pulling the script out of the $(document).ready() function, though, and adding the class to the html element rather than to the body (as I wrote about here). This way you can avoid the flash of unstyled content that can occur with complex DOM structures.


add your comment