Detects the browser and its version.
// Browser detection // Internet Explorer var ie = document.all != null; //ie4 and above var ie5 = document.getElementById && document.all; var ie6 = document.getElementById && document.all&&(navigator.appVersion.indexOf("MSIE 6.")>=0); // Netscape var ns4 = document.layers != null; var ns6 = document.getElementById && !document.all; var ns = ns4 || ns6; // Firefox var ff = !document.layers && !document.all; // Opera var op = navigator.userAgent.indexOf("opera")>0; var op7 = op && operaVersion() <= 7; var op8 = op && operaVersion() >= 8; // Detects the Opera version function operaVersion() { agent = navigator.userAgent; idx = agent.indexOf("opera"); if (idx>-1) { return parseInt(agent.subString(idx+6,idx+7)); } }
Comments
Subscribe to comments
You need to login to post a comment.

function cssbrowserselector(u){var ua = u.toLowerCase(),is=function(t){return ua.indexOf(t)>-1;},g='gecko',w='webkit',s='safari',h=document.getElementsByTagName('html')[0],b=[(!(/opera|webtv/i.test(ua))&&/msie\s(\d)/.test(ua))?('ie ie'+RegExp.$1):is('firefox/2')?g+' ff2':is('firefox/3')?g+' ff3':is('gecko/')?g:/opera(\s|\/)(\d+)/.test(ua)?'opera opera'+RegExp.$2:is('konqueror')?'konqueror':is('chrome')?w+' chrome':is('applewebkit/')?w+' '+s+(/version\/(\d+)/.test(ua)?' '+s+RegExp.$1:''):is('mozilla/')?g:'',is('j2me')?'mobile':is('iphone')?'iphone':is('ipod')?'ipod':is('mac')?'mac':is('darwin')?'mac':is('webtv')?'webtv':is('win')?'win':is('freebsd')?'freebsd':(is('x11')||is('linux'))?'linux':'','js']; c = b.join(' '); h.className += ' '+c; return c;}; cssbrowserselector(navigator.userAgent);
With just one line and less than 1kb which empower browser selectors. It gives you the ability to write specific CSS and JS code for each operating system and each browser.
If you want to target ie with different css then use conditional comments and css. There's no need for js.
http://snipplr.com/view/18180/targeting-ie-using-conditional-comments-and-just-one-stylesheet/
If you are using jQuery, you can use jQuery.browser to get browser details.
In this example (http://eisabainyo.net/weblog/2010/09/01/10-useful-jquery-snippets/), I checked if the browser is IE 6 and displayed appropriate message if it is.
Here's what I use for browser sniffing. http://snipplr.com/view/9096/jquery-common-browser-sniffing/