AndreaNobili
AndreaNobili

Reputation: 42957

Why using window.navigator.userAgent to retrieve the browser Explorer 11 is recognized as Mozilla? How retrieve the user angent and version?

I am pretty new in JavaScript and JQuery and I am going crazy trying to implement a simple script that detect if the browser is Internet Explorer and its version.

So I am doing something like this:

$( document ).ready(function() {

    //alert(navigator.appName);
    //alert(navigator.appCodeName);
    //alert(navigator.appVersion);
    //alert(navigator.platform);
    //alert(window.navigator.userAgent);

    var ua = window.navigator.userAgent;
    var msie = ua.indexOf("MSIE ");

    console.log("USER AGENT: " + ua);
    console.log("MSIE: " + msie);
});

The problem is that running the page into Explorer 11 into the console log I obtain this messages:

USER AGENT: Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; .NET4.0E; .NET4.0C; .NET CLR 3.5.30729; .NET CLR 2.0.50727; .NET CLR 3.0.30729; InfoPath.3; GWX:QUALIFIED; ASU2JS; rv:11.0) like Gecko

MSIE: -1

How is it possible that it is recognized as Mozilla and not as IE?

I need to recognize if the browser is Internet Explorer and its version.

How can I do this operation?

Upvotes: 3

Views: 12119

Answers (3)

ITomas
ITomas

Reputation: 839

IE 11 broke all client-side check scripts with its release. As you said, it now reports as "Mozilla" and no longer reports the MSIE. As far as what I remember, the decision they took was to do this because IE11 is supposed to be based more on the Gecko engine as opposed to Mozilla. To illustrate this, Microsoft decided to change the User-Agent string to something different. The best way I know to test for IE11 is to check for "Trident/7.0" which is supposed to be informed by all IE11 browsers.

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

Anyway, as many will recommend, it is better to check for functionalities rather than relying on browser checking.

Upvotes: 4

Vishal Kiri
Vishal Kiri

Reputation: 1306

Please try to below code.

var ua = window.navigator.userAgent;
    var msie = ua.indexOf("MSIE ");
    var rv = -1;

    if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./))      // If Internet Explorer, return version number
    {               

        if (isNaN(parseInt(ua.substring(msie + 5, ua.indexOf(".", msie))))) {
            //For IE 11 >
            if (navigator.appName == 'Netscape') {
                var ua = navigator.userAgent;
                var re = new RegExp("Trident/.*rv:([0-9]{1,}[\.0-9]{0,})");
                if (re.exec(ua) != null) {
                    rv = parseFloat(RegExp.$1);
                    alert(rv);
                }
            }
            else {
                alert('otherbrowser');
            }
        }
        else {
            //For < IE11
            alert(parseInt(ua.substring(msie + 5, ua.indexOf(".", msie))));
        }
        return false;
    }}

Upvotes: 0

FelisCatus
FelisCatus

Reputation: 5334

According to Microsoft, IE11's user agent string is specially designed to trick UA parsers into recognizing it as something else. Source: http://blogs.msdn.com/b/ieinternals/archive/2013/09/21/internet-explorer-11-user-agent-string-ua-string-sniffing-compatibility-with-gecko-webkit.aspx

I will repeat this even if it is mentioned in the article above. If you want to do UA sniffing, please think twice. Feature detection is the recommended way to deal with browser compatibility. See the article for more on this.

Upvotes: 6

Related Questions