Skip to content

Instantly share code, notes, and snippets.

@kynatro
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kynatro/ce184d6a180f6e04a4f1 to your computer and use it in GitHub Desktop.
Save kynatro/ce184d6a180f6e04a4f1 to your computer and use it in GitHub Desktop.
100% Reliable no UserAgent, JavaScript IE Detection
// IE version detection - 100% reliable since it depends on IE unique functionality
var ie = (function(){
// IE <= 9
var undef,
v = 3,
div = document.createElement('div'),
all = div.getElementsByTagName('i');
while (
div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->',
all[0]
);
// IE 10
if(Function('/*@cc_on return document.documentMode===10@*/')()){v=10;}
return v > 4 ? v : undef;
})();

IE JavaScript Detector

Usage:

Just include this code in your JavaScript somewhere and get the version of IE just by referencing the ie variable. The variable will return either a number (4-10) or undefined.

Example:

When viewed in IE4-IE10

alert(ie); // Alerts the IE version as an integer (4-10)

When viewed in IE11

Microsoft deprecated their conditional comment support starting with IE 10. There is a specialized "hack" taking advantage of a specialized JavaScript conditional comment support language that allows us to do IE detection for version 10 that is built in, but that technique has not been tested with IE11. The nice thing is, it shouldn't be necessary there since Microsoft finally decided to take their heads out of the sand and start trying to play along with the rest of the internet with version 11.

alert(ie); // Alerts undefined

When viewed in Chrome, Firefox or Safari

alert(ie); // Alerts undefined

Testing for IE

if(ie){
  // Do some stuff for poor IE users
} else {
  // Do stuff for everyone else on the internet
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment