Forked from cowboy/detect IE and version number through injected conditional comments.js
Created
April 6, 2010 20:03
-
-
Save scottjehl/358029 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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 = {}, | |
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 ]; | |
}; | |
})(); | |
//minified: | |
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? | |
isIE(); | |
//is it IE6? | |
isIE(6); | |
//is it less than or equal to IE 7? | |
isIE(7,'lte'); |
A similar trick over here: http://gist.github.com/527683
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.)
An IE-check ?
Have you considered ?:
var IE='\v'=='v';
http://ajaxian.com/archives/ievv
http://webreflection.blogspot.com/2009/01/32-bytes-to-know-if-your-browser-is-ie.html
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You are missing the 'undefined' argument in the minified version. -- just say'n.