When I want to detect IE I use this code:
function getInternetExplorerVersion()
{
var rv = -1;
if (navigator.appName == 'Microsoft Internet Explorer')
{
var ua = navigator.userAgent;
var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
if (re.exec(ua) != null)
rv = parseFloat( RegExp.$1 );
}
return rv;
}
function checkVersion()
{
var msg = "You're not using Internet Explorer.";
var ver = getInternetExplorerVersion();
if ( ver > -1 )
{
msg = "You are using IE " + ver;
}
alert( msg );
}
But IE11 is returning "You're not using Internet Explorer". How can I detect it?
I used the
onscroll
event at the element with the scrollbar. When triggered in IE, I added the following validation:solution :
Use
!(window.ActiveXObject) && "ActiveXObject" in window
to detect IE11 explicitly.To detect any IE (pre-Edge, "Trident") version, use
"ActiveXObject" in window
instead.Angular JS does this way.
msie will be positive number if its IE and NaN for other browser like chrome,firefox.
why ?
As of Internet Explorer 11, the user-agent string has changed significantly.
refer this :
msdn #1 msdn #2
I'm using a simpler method:
The navigator global object has a property touchpoints, in Internet Exlorer 11 is called msMaxTouchPoints tho.
So if you look for:
You will find Internet Explorer 11.
Get IE Version from the User-Agent
How it works: The user-agent string for all IE versions includes a portion "MSIE space version" or "Trident other-text rv space-or-colon version". Knowing this, we grab the version number from a
String.match()
regular expression. Atry-catch
block is used to shorten the code, otherwise we'd need to test the array bounds for non-IE browsers.Note: The user-agent can be spoofed or omitted, sometimes unintentionally if the user has set their browser to a "compatibility mode". Though this doesn't seem like much of an issue in practice.
Get IE Version without the User-Agent
How it works: Each version of IE adds support for additional features not found in previous versions. So we can test for the features in a top-down manner. A ternary sequence is used here for brevity, though
if-then
andswitch
statements would work just as well. The variableie
is set to an integer 5-11, or 1 for older, or 99 for newer/non-IE. You can set it to 0 if you just want to test for IE 1-11 exactly.Note: Object detection may break if your code is run on a page with third-party scripts that add polyfills for things like
document.addEventListener
. In such situations the user-agent is the best option.Detect if the Browser is Modern
If you're only interested in whether or not a browser supports most HTML 5 and CSS 3 standards, you can reasonably assume that IE 8 and lower remain the primary problem apps. Testing for
window.getComputedStyle
will give you a fairly good mix of modern browsers, as well (IE 9, FF 4, Chrome 11, Safari 5, Opera 11.5). IE 9 greatly improves on standards support, but native CSS animation requires IE 10.