Detect IE11 using JavaScript/jQuery

In this post, find JavaScript/jQuery code to detect IE 11. Your old code detect IE browser either using navigator.userAgent or $.browser.msie will not work for IE 11 as the User Agent string for IE 11 is changed.

Previously I had posted about Detect Browsers using jQuery, but with the release of jQuery 1.9 $.browser feature was removed. But to support legacy code, they have released jQuery Migrate plugin to detect deprecated and removed features, or to restore old features for those sticky situations where you need old code to run with new jQuery. You can read more about How to migrate older jQuery code to jQuery 1.9+

You may also like:

The user agent string for IE11 on Windows 8.1 is,

Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko

And if you compare with earlier version of IE then you will find the "MSIE" token is no longer present. For the earlier version of IE, "MSIE" token was present. Take a look at IE10 user agent string.

Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)

So the code to find index of "MSIE" in navigator.userAgent will not work for IE11.

var sAgent = window.navigator.userAgent;
var Idx= sAgent.indexOf("MSIE");

So to detect IE11, all you need is to look for "Trident/7.0" in user agent string. Here is updated code to detect all versions of IE including IE 11.

function GetIEVersion() {
  var sAgent = window.navigator.userAgent;
  var Idx = sAgent.indexOf("MSIE");

  // If IE, return version number.
  if (Idx > 0) 
    return parseInt(sAgent.substring(Idx+ 5, sAgent.indexOf(".", Idx)));

  // If IE 11 then look for Updated user agent string.
  else if (!!navigator.userAgent.match(/Trident/7./)) 
    return 11;

  else
    return 0; //It is not IE
}

if (GetIEVersion() > 0) 
   alert("This is IE " + GetIEVersion());
else 
   alert("This is not IE.");

Feel free to contact me for any help related to jQuery, I will gladly help you.



Responsive Menu
Add more content here...