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

11,082

Solution 1

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.

Solution 2

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.

Share:
11,082
AndreaNobili
Author by

AndreaNobili

Updated on June 04, 2022

Comments

  • AndreaNobili
    AndreaNobili about 2 years

    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?