January 13, 2012

Add class for each Internet Explorer Version

Edit: Now supports IE10! There is a little code snippet to detect the current version of IE (or if its not IE at all):
var ie = (function(){
 
    var undef,
        v = 3,
        div = document.createElement('div'),
        all = div.getElementsByTagName('i');
 
    do {
        div.innerHTML = ''; 
    } while(all[0]);
    /*@cc_on;if(v<5)v=10;@*/
    return v > 4 ? v : undef ;
 
}());
Its pretty useful; and if you want to have dynamic class names for each IE version, just do this after the initial <body> tag (with jQuery):
if(ie){$('body').addClass("ie"+ie)}
Or in plain Javascript:
if(ie){document.body.className += " ie"+ie}
Now you can easily apply CSS rules for those ugly browsers:
.ie8 #menu, ie7 #menu{
 position:absolute;  
}
.ie6 #top{
 display:none;
}
You get the idea.

No comments:

Post a Comment

You can use [CODE][/CODE] to post Javascript code.
You can start a sentence with ">" to make a quotation.