Skip to content

Instantly share code, notes, and snippets.

@ahawthorne
Last active December 17, 2015 20:19
Show Gist options
  • Save ahawthorne/5666557 to your computer and use it in GitHub Desktop.
Save ahawthorne/5666557 to your computer and use it in GitHub Desktop.
JavaScript to detect IE version using conditional tags and add classes to the html tag.
window.ie = (function() {
var undef,
v = 3,
htmlClass = document.documentElement.className,
div = document.createElement('div'),
all = div.getElementsByTagName('span');
do {
div.innerHTML = '<!--[if gt IE ' + (++v) + ']><span></span><!endif]-->';
} while (all[0]);
return v > 4 ? v : undef;
})();
(function() {
var ieClass = '';
// add classes to html
switch (window.ie) {
case 9:
ieClass = 'ie9';
break;
case 8:
ieClass = 'lt-ie9 ie8';
break;
case 7:
ieClass = 'lt-ie9 lt-ie8 ie7';
break;
}
document.documentElement.className = ieClass;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment