javascript 为什么使用 window.navigator.userAgent 检索浏览器 Explorer 11 被识别为 Mozilla?如何检索用户代理和版本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32435511/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 15:16:40  来源:igfitidea点击:

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

javascriptjqueryhtmluser-agent

提问by AndreaNobili

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 Explorerand its version.

我是 JavaScript 和 JQuery 的新手,尝试实现一个简单的脚本来检测浏览器是否是Internet Explorer及其版本,我快疯了。

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 11into the console log I obtain this messages:

问题是将页面运行到Explorer 11到控制台日志中,我收到了以下消息:

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?

它怎么可能被识别为 Mozilla 而不是 IE?

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

我需要识别浏览器是否为 Internet Explorer 及其版本。

How can I do this operation?

我怎样才能做这个手术?

回答by FelisCatus

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

据微软称,IE11 的用户代理字符串专门用于欺骗 UA 解析器将其识别为其他内容。来源: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.

即使在上面的文章中提到过,我也会重复这一点。如果你想做 UA 嗅探,请三思。特征检测是处理浏览器兼容性的推荐方法。有关更多信息,请参阅文章。

回答by ITomas

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.

IE 11 的发布打破了所有客户端检查脚本。正如您所说,它现在报告为“Mozilla”,不再报告 MSIE。据我所知,他们做出的决定是这样做,因为 IE11 应该更多地基于 Gecko 引擎而不是 Mozilla。为了说明这一点,Microsoft 决定将 User-Agent 字符串更改为不同的内容。我所知道的测试 IE11 的最佳方法是检查“Trident/7.0”,它应该由所有 IE11 浏览器通知。

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

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

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

无论如何,正如许多人所建议的那样,最好检查功能而不是依赖浏览器检查。

回答by Vishal Kiri

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.);
                    alert(rv);
                }
            }
            else {
                alert('otherbrowser');
            }
        }
        else {
            //For < IE11
            alert(parseInt(ua.substring(msie + 5, ua.indexOf(".", msie))));
        }
        return false;
    }}