Skip to main content

CSS Browser detection using jQuery instead of hacks

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

Popular posts from this blog

REST / AJAX calls from within a Jaggery script

<% var mySecureToken = FnMakeRequestCall("https://10.299.99.99:8245/token","POST","Basic RlN4S2RrZEpNN3VaYWhHN0NFcEtlaTZEa3RzYTpXbmUxd29seHp2UTNSQ2RZbXhUUTJ2WkJTd0Fh","application/x-www-form-urlencoded; charset=UTF-8",""application/json; charset=utf-8","grant_type=password&username=pcsadmin&password=pcsadmin");  print(mySecureToken); function FnMakeRequestCall(URL, METHOD, BASICAUTH, CONTENTTYPE, ACCEPTTYPE, INPUTDATA){ if(BASICAUTH){ //var VarBasicAuthCode = util.FnCreateBasicAuthentication(VARSKYSPARKUSERNAME,VARSKYSPARKPASSWORD); var VarBasicAuthCode = session.get('authToken'); } xhr = new XMLHttpRequest(); xhr.open(METHOD, URL); if(BASICAUTH){ xhr.setRequestHeader("Authorization" , VarBasicAuthCode); } xhr.setRequestHeader("Content-Type", CONTENTTYPE); xhr.setRequestHeader("Accept", ACCEPTTYPE); xhr.send(INPUTDATA); var VarRes

MySQL table and want to build a XML file with it in order to make a RSS feed.

<? php $dom = new DOMDocument() ; include "connection. php "; $queryz = "SELECT * FROM ticker"; $resultz = mysql_query($queryz) or die('Error, query failed'); $row = mysql_fetch_assoc($resultz); $ rss = $dom->createElement(' rss '); while($row = mysql_fetch_array( $resultz )) { $item = $dom->createElement("item"); $item->appendChild($dom->createElement("title", $row['item_title'])); $item->appendChild($dom->createElement("pubDate",$row['item_pubDate'])); $item->appendChild($dom->createElement("description",$row['item_description'])); $item->appendChild($dom->createElement("link",$row['item_link'])); $ rss ->appendChild($item); } $dom->appendChild($ rss ); $dom->save("myxml.xml"); OR <?php header('Content-Type: text/xml; charset=UTF-8'); $dom = new DOMDocument() ;