Skip to content

Instantly share code, notes, and snippets.

@vishaltelangre
Last active December 15, 2015 20:41
Show Gist options
  • Save vishaltelangre/5320491 to your computer and use it in GitHub Desktop.
Save vishaltelangre/5320491 to your computer and use it in GitHub Desktop.
Fuckin' IE Detection, the "hawt" way!
// @author: Vishal Telangre <the@vishaltelangre.com>
// Published under MIT license.
//
// Requires: jQuery
//
// Usage:
//
// ieInspector.isIe
// -- returns
// - true for IE browsers
// - false for non-IE browsers
// ieInspector.ieVersion
// -- returns
// - "6.0", "7.0", "8.0", etc. for IE browsers
// - undefined for non-IE browsers
window.ieInspector = (function() {
var browser = $.browser;
var self = this;
return {
isIe: function() {
return browser.hasOwnProperty("msie");
}(),
ieVersion: function(){
if (browser.hasOwnProperty("msie"))
return browser.version;
else
return undefined;
}()
};
}());
// ----------------------------------------------------------
// A short snippet for detecting versions of IE in JavaScript
// without resorting to user-agent sniffing
// ----------------------------------------------------------
// If you're not in IE (or IE version is less than 5) then:
// ie === undefined
// If you're in IE (>=5) then you can determine which version:
// ie === 7; // IE7
// Thus, to detect IE:
// if (ie) {}
// And to detect the version:
// ie === 6 // IE6
// ie > 7 // IE8, IE9 ...
// ie < 9 // Anything less than IE9
// ----------------------------------------------------------
// UPDATE: Now using Live NodeList idea from @jdalton
var ie = (function(){
var undef,
v = 3,
div = document.createElement('div'),
all = div.getElementsByTagName('i');
while (
div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->',
all[0]
);
return v > 4 ? v : undef;
}());
// REFERENCE: https://gist.github.com/padolsey/527683#file-gistfile1-js
var ie = (function(){
var undef, v = 3, div = document.createElement('div');
// the while loop is used without an associated block: {}
// so, only the condition within the () is executed.
// semicolons arent allowed within the condition,
// so a comma is used to stand in for one
// basically allowing the two separate statements
// to be evaluated sequentially.
while (
div.innerHTML = '<!--[if gt IE '+(++v)+']><i></i><![endif]-->',
div.getElementsByTagName('i')[0]
);
// each time it's evaluated, v gets incremented and
// tossed into the DOM as a conditional comment
// the i element is then a child of the div.
// the return value of the getEBTN call is used as
// the final condition expression
// if there is an i element (the IE conditional
// succeeded), then getEBTN's return is truthy
// and the loop continues until there is no
// more i elements.
// In other words: ** MAGIC**
return v > 4 ? v : undef;
}());
// REFERENCE: https://gist.github.com/padolsey/527683#comment-7599
// For all how what to detect IE10:
(function() {
"use strict";
var tmp = (document["documentMode"] || document.attachEvent) && "ev"
, msie = tmp
&& (tmp = window[tmp + "al"])
&& tmp("/*@cc_on 1;@*/")
&& +((/msie (\d+)/i.exec(navigator.userAgent) || [])[1] || 0)
;
return msie || void 0;
})();
// REFERENCE: https://gist.github.com/padolsey/527683/#comment-768383
var ie = (function(){
// for-loop saves characters over while
for( var v = 3,
// b just as good as a div with 2 fewer characters
el = document.createElement('b'),
// el.all instead of el.getElementsByTagName('i')
// empty array as loop breaker (and exception-avoider) for non-IE and IE10+
all = el.all || [];
// i tag not well-formed since we know that IE5-IE9 won't mind
el.innerHTML = '<!--[if gt IE ' + (++v) + ']><i><![endif]-->',
all[0];
);
// instead of undefined, returns the documentMode for IE10+ compatibility
// non-IE will still get undefined as before
return v > 4 ? v : document.documentMode;
}() );
// MINIFIED VERSION:
var ie=function(){for(var a=3,b=document.createElement("b"),c=b.all||[];b.innerHTML="<!--[if gt IE "+ ++a+"]><i><![endif]-->",c[0];);return 4<a?a:document.documentMode}();
// REFERENCE: https://gist.github.com/padolsey/527683/#comment-786682
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment