Skip to content

Instantly share code, notes, and snippets.

@getify
Created November 22, 2010 16:48
Show Gist options
  • Save getify/710231 to your computer and use it in GitHub Desktop.
Save getify/710231 to your computer and use it in GitHub Desktop.
how to detect IE from JavaScript using conditional comments
// stripped down version just detecting IE
(function(global){
global._isIE = false;
try {
var div = document.createElement("div");
div.innerHTML = "<!--[if IE]><i></i><![endif]-->";
global._isIE = (div.getElementsByTagName("i").length > 0);
} catch(err) { }
})(window);
// fuller version with version detection (in this case, IE6)
(function(global){
global._isIE = false;
global._isIE6 = false;
try {
var div = document.createElement("div"),
all = div.getElementsByTagName("i")
;
div.innerHTML = "<!--[if IE]><i></i><![endif]--><!--[if IE 6]><i></i><![endif]-->";
if (all[0]) global._isIE = true;
if (all[1]) global._isIE6 = true;
} catch(err) { }
})(window);
@getify
Copy link
Author

getify commented Nov 22, 2010

FYI: the "original" gist (from james padolsey) with a more capable version of this technique for any IE versions, etc: https://gist.github.com/527683

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment