Instantly share code, notes, and snippets.

What would you like to do?
//detect Internet Explorer and version number through injected conditional comments (no UA detect, no need for cond. compilation / jscript check)
//version arg is for IE version (optional)
//comparison arg supports 'lte', 'gte', etc (optional)
var isIE = (function(undefined){
var doc = document,
doc_elem = doc.documentElement,
cache = {},
return function( version, comparison ) {
if(/*@cc_on!@*/true){return false;}
var key = [ comparison || '', 'IE', version || '' ].join(' ');
if ( cache[ key ] === undefined ) {
elem = elem || doc.createElement( 'B' );
elem.innerHTML = '<!--[if '+ key +']><b></b><![endif]-->';
cache[ key ] = !!elem.getElementsByTagName( 'b' ).length;
return cache[ key ];
var isIE=(function(){var doc=document,doc_elem=doc.documentElement,cache={},elem;return function(version,comparison){if(/*@cc_on!@*/true){return false;}var key=[comparison||"","IE",version||""].join(" ");if(cache[key]===undefined){elem=elem||doc.createElement("B");elem.innerHTML="<!--[if "+key+"]><b></b><![endif]-->";cache[key]=!!elem.getElementsByTagName("b").length;}return cache[key];};})();
//is it IE?
//is it IE6?
//is it less than or equal to IE 7?

This comment has been minimized.

westonruter commented Apr 6, 2010

Note that cache[ key ] === undefined could blow up since it is currently legal to do undefined = true; therefore, modifying the anonymous function declaration to accept a single argument called undefined and then not passing it in will ensure that it is actually undefined. I believe this was first popularized by jQuery 1.4.

-- var isIE = (function(){
++ var isIE = (function(undefined){

This comment has been minimized.


scottjehl commented Apr 6, 2010

great point and nice catch :)


This comment has been minimized.

cowboy commented Apr 8, 2010

Yeah, my bad. I do this in all my plugins, but forgot to do it here!


This comment has been minimized.

fifteen3 commented Aug 20, 2010

You are missing the 'undefined' argument in the minified version. -- just say'n.


This comment has been minimized.

paulirish commented Oct 30, 2010

A similar trick over here:


This comment has been minimized.

ELLIOTTCABLE commented Dec 6, 2010

A cleaner way than that unused function argument, IMHO:

-- var isIE = (function(){
++ var isIE = (function(){var undefined;

Declaring a new variable named undefined overrides whatever it might be defined in the enclosing scope; and obviously, leaving it undefined makes it, well, function as undefined is intended to. (Personally, because I like allowing people to do evil hacks with undefined if they want, I use var u; in a new scope, instead, in most of my code.)


This comment has been minimized.

Lennie commented Jul 31, 2011

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