Question

Just stumbled upon an issue. When trying to detect IE 11 (the beta version currently on air) using Jquery, the result is 'firefox'. The same code detect IE 10. I need to know what browser the user is using in order to display different instructions.

I am testing in Oracle VirtualBox if it matters. The OS is Win 7.

Here's the code:

<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script>
var browser = function() { 
if ($.browser.msie) return "ie";
var ua = navigator.userAgent.toLowerCase();
if ($.browser.mozilla/* && /firefox/.test(ua)*/) return "firefox"; 
if (/chrome/.test(ua)) return "chrome";
return /*"#"*/'unknown';
} ();

alert (browser); // This return firefox
alert ($.browser.version); // This returns 11.0 - the CORRECT version of IE
</script>

As you can see, Jquery can find the browser version, but not the browser name. Any idea how to bypass it?

Was it helpful?

Solution

The final solution:

if (!!navigator.userAgent.match(/Trident\/7\./))
  return "ie";  

We can only hope that the release version will act the same.

OTHER TIPS

It's for compatibility reasons. Client code often performs browser detection instead of feature detection (which is a poor practice). So in an effort to make sure that clients properly use all of IE 11's capabilities Microsoft has made it so that IE 11 will report that it's Mozilla compatible.

So instead of doing browser detection, do feature detection. See Browser detection versus feature detection. There's some great libraries for that, with Modernizr probably being the most well known (and Microsoft ships it as part of the ASP.NET templates in Visual Studio).

See MSDN blog about IE 11 User Agent Strings.

The purpose of jQuery Migrate is to allow old badly-written code to run, not to encourage writing new badly-written code. Since that old badly-written code was created long before IE11 was released, it doesn't know about IE11 anyway and will probably misbehave regardless. The jQuery Migrate plugin won't be changed to detect IE11. If you are writing new code, don't use browser detection. Instead, use feature detection.

jQuery.browser is long deprecated and has been removed, you should use $.support or a better tool like Modernizr

If you just need to know whether its IE following is the code

function isIE() {
var ua = window.navigator.userAgent; //Check the userAgent property of the window.navigator object
var msie = ua.indexOf('MSIE '); // IE 10 or older
var trident = ua.indexOf('Trident/'); //IE 11
if(msie > 0)
{
    console.log("IE less than 11 ");
}
else if(trident > 00)
{
    console.log("IE more than 11 ");
}
return (msie > 0 || trident > 0);
}
console.log(isIE());//returns true for any IE version.

While if you need to check version together you need to check the document.documentMode

function isIE11() 
{
    var ua = window.navigator.userAgent; //Check the userAgent property of the window.navigator object
    var msie = ua.indexOf('MSIE '); // IE 10 or older
    var trident = ua.indexOf('Trident/'); //IE 11
    if(msie > 0)
    {
        console.log("IE less than 11 ");
    }
    else if(trident > 00)
    {
        console.log("IS 11 ");
    }
    if(msie > 0 || trident > 0 && document.documentMode)
    {
        console.log("document.documentMode ::",document.documentMode)//document.documentMode must be 11 or Edge
        if(document.documentMode == 11)
        {
        return true;
        }
    }
    return false;
}
console.log(isIE11());

To see/change the version in IE you can go to emulation in the developer console.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top