Created
June 22, 2017 22:47
-
-
Save justinallen/a4b9b493d88374ee4e67c13f37d674fd to your computer and use it in GitHub Desktop.
IE Detection in JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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