Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
IE Detection in JavaScript
// just a slimmed-down implementation of browser testing for IE in JavaScript
// based on code I found online
/**
* detect IE
* returns version of IE or false, if browser is not Internet Explorer
*/
function detectIE() {
var ua = window.navigator.userAgent;
var msie = ua.indexOf('MSIE ');
if (msie > 0) {
// IE 10 or older => return version number
return parseInt(ua.substring(msie + 5, ua.indexOf('.', msie)), 10);
}
var trident = ua.indexOf('Trident/');
if (trident > 0) {
// IE 11 => return version number
var rv = ua.indexOf('rv:');
return parseInt(ua.substring(rv + 3, ua.indexOf('.', rv)), 10);
}
var edge = ua.indexOf('Edge/');
if (edge > 0) {
// Edge (IE 12+) => return version number
return parseInt(ua.substring(edge + 5, ua.indexOf('.', edge)), 10);
}
// other browser
return false;
}
// Get IE or Edge browser version
var ieVersion = detectIE();
if (ieVersion === false) {
console.log('NOT ie/edge');
} else if (ieVersion >= 12) {
console.log('edge ' + ieVersion);
} else {
console.log('ie ' + ieVersion);
ieTest();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.