Skip to content

Instantly share code, notes, and snippets.

@mattpavelle
Last active December 18, 2015 02:08
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 mattpavelle/5708358 to your computer and use it in GitHub Desktop.
Save mattpavelle/5708358 to your computer and use it in GitHub Desktop.
Check the browser userAgent and determine if it is IE8+ or IE7- (counting compatibility mode as the +, not the -)
// usually code that works in IE8/9/10 will also work in IE8/9/10 in compatibility mode, but IE reports itself as IE7
// or some such when running in compatibility mode... so let's pretend that IE9 in IE7 compatibility mode is IE9
function checkVersion() {
var msg = "Not IE";
var ver = -1; // assume failure
var ua = navigator.userAgent;
if (navigator.appName == 'Microsoft Internet Explorer') {
var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
if (re.exec(ua) != null)
ver = parseFloat( RegExp.$1 );
}
if (ver > -1) {
// the version is usually valid
if (ver >= 8.0)
msg = "IE8+";
// unless we're in Compatibility Mode
else if (ua.indexOf('Trident') !== -1)
msg = "IE8+CM";
else
msg = "IE7-";
}
alert( msg );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment