Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save cowboy/357789 to your computer and use it in GitHub Desktop.
Save cowboy/357789 to your computer and use it in GitHub Desktop.
//EnhanceJS isIE test idea
//detect IE 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(){
var doc = document,
doc_elem = doc.documentElement,
cache = {},
elem;
return function( version, comparison ) {
var key = [ comparison || '', 'IE', version || '' ].join(' ');
if ( cache[ key ] === undefined ) {
elem = elem || doc.createElement( 'B' );
elem.innerHTML = '<!--[if '+ key +']><b id="iecctest"></b><![endif]-->';
doc_elem.appendChild( elem );
cache[ key ] = !!doc.getElementById( 'iecctest' );
doc_elem.removeChild( elem );
}
return cache[ key ];
};
})();
// minified:
var isIE=(function(){var e=document,d=e.documentElement,a={},c;return function b(f,h){var g=[h||"","IE",f||""].join(" ");if(a[g]===undefined){c=c||e.createElement("B");c.innerHTML="<!--[if "+g+']><b id="iecctest"></b><![endif]-->';d.appendChild(c);a[g]=!!e.getElementById("iecctest");d.removeChild(c)}return a[g]}})();
// See http://jsfiddle.net/cowboy/75GBD/
//is it IE?
isIE();
//is it IE6?
isIE(6);
//is it less than or equal to IE 6?
isIE(7,'lte');
@cowboy
Copy link
Author

cowboy commented Apr 6, 2010

Benefits:

  • results are memoized
  • only a single dom element is created

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