public
Last active — forked from peteboere/ie-mobile-detect.js

JavaScript IE Version Detection, IE6-10 and IE Mobile

  • Download Gist
ie-6-9-detect.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
// ----------------------------------------------------------
// A short snippet for detecting versions of IE:
// Uses a combination of object detection and user-agent
// sniffing.
// ----------------------------------------------------------
// If you're not in IE then:
// ie === NaN // falsy
// If you're in IE 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
// script needs to be wrapped on conditional comments to exclude > ie9
// appends class of "ieX" where X is version number, to document's html element
// ie8 demo http://dev.bowdenweb.com/ua/browsers/ie/ie8-detection.html
// ie9 demo http://dev.bowdenweb.com/ua/browsers/ie/ie9-detection.html
 
var ie = ( !!window.ActiveXObject && +( /msie\s(\d+)/i.exec( navigator.userAgent )[1] ) ) || NaN;
if (ie === 6) {
document.documentElement.className+=' ie6';
} else if (ie === 7) {
document.documentElement.className+=' ie7';
} else if (ie === 8) {
document.documentElement.className+=' ie8';
} else if (ie === 9) {
document.documentElement.className+=' ie9';
}
ie-mobile-detect.js
JavaScript
1 2 3
// The same thing but for IE Mobile instead.
 
var ieMobile = ( !! window.ActiveXObject && +( /IEMobile\/(\d+\.?(\d+)?)/.exec( navigator.userAgent )[1] ) ) || NaN;
ie10-detect.js
JavaScript
1 2 3 4 5 6 7
// ie10 doesn't support conditional comments but it does support @cc_on
// @cc_on activates conditional comments within a script
// appends class of "ie10" to the document's html element
// http://msdn.microsoft.com/en-us/library/8ka90k2e%28v=vs.94%29.aspx
// demo http://dev.bowdenweb.com/ua/browsers/ie/ie10-detection-via-cc.html
 
<!--[if !IE]><!--><script>if(/*@cc_on!@*/false){document.documentElement.className+=' ie10';}</script><!--<![endif]-->

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.