Skip to content

Instantly share code, notes, and snippets.

@johnantoni
Created August 23, 2010 14:52
Show Gist options
  • Save johnantoni/545630 to your computer and use it in GitHub Desktop.
Save johnantoni/545630 to your computer and use it in GitHub Desktop.
dean edward ie conditional detect
// http://jsperf.com/ie
// Conditional Comment
ie = (function() {
var v = 3,
div = document.createElement('div');
while (
div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->', div.getElementsByTagName('i')[0]);
window.status = v;
return (v > 4) ? v : false;
})();
// Conditional Compilation
ie = ( /*@cc_on!@*/ false) && (parseInt(navigator.userAgent.match(/msie\s(\d+)/i)[1]));
/*
Dean Edwards’s code is not equivalent to this. Unlike conditional comments, conditional compilation cannot detect IE versions: it can give you the version of JScript and which OS the browser is running on, but that’s about it. If you absolutely have to detect the version of IE, conditional comments are the way to go, though you should be using feature detection instead for pretty much everything.
One final point is that turning conditional compilation on (which is what Dean’s code does) can have unexpected side effects: a comment such as //@TODO Fix this code will throw an error in IE if and only if conditional compilation is turned on.
*/
@johnantoni
Copy link
Author

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