Detect IE11 using JavaScript/jQuery
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:
- Detect Mobile Browsers using jQuery
- Source Map -new feature of jQuery 1.9
- Tools for Cross Browser Testing
- Fix for "IE Select DropDown with Fixed width issue" using jQuery
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.