为什么在 google chrome 浏览器中尝试时,javaScript 中的“navigator.userAgent”会返回字符串“Mozilla”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7050521/
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-08-24 00:36:22  来源:igfitidea点击:

why Does "navigator.userAgent" in javaScript returns the String "Mozilla" when tried in a google chrome borwser?

javascriptnavigatorbrowserid

提问by phihag

Im developing a javaScript code and I want to determine the version and brand of the client's browser, here's the piece of code I'm using to do so :

我正在开发一个 javaScript 代码,我想确定客户端浏览器的版本和品牌,这是我用来这样做的一段代码:

var browserName ; 
function BrowserCheckin () 
{

    if(navigator.userAgent.indexOf("Mozilla") > 0 )
        browserName = "Mozilla" ;
    if (navigator.userAgent.indexOf("MSIE") > 0 )
        browserName = "InternetExplorer";
    if (navigator.userAgent.indexOf("Chrome") > 0)
        browserName= "Google Chrome" ; 
    if(navigator.userAgent.indexOf("Opera") > 0 ) 
        browserName = "Opera" ; 
    document.write("<h1>"  + browserName + "</h1>") ;
}

but when i run my code using "Google Chrome" , the useAgent property returns a string containting :

但是当我使用“Google Chrome”运行我的代码时,useAgent 属性返回一个包含以下内容的字符串:

"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.112 Safari/534.30 "

"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.112 Safari/534.30 "

But I don't get what that "Mozilla/5.0" is doing there , anyone has any Idea ?
(and one more thing , I use Linux as my Operating System)

但我不明白“Mozilla/5.0”在那里做什么,有人有什么想法吗?
(还有一件事,我使用 Linux 作为我的操作系统)

Thanks in advance :)

提前致谢 :)

回答by phihag

For historical reasons, nearly all browsers (except Opera) prepend Mozilla/to their user agent.

由于历史原因,几乎所有浏览器(Opera 除外)都预先设置Mozilla/了它们的用户代理。

For example, here are some common user agents:

例如,以下是一些常见的用户代理:

Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/534.30 (KHTML, like Gecko)
            Chrome/12.0.742.112 Safari/534.30                    # Chrome 12
Mozilla/5.0 (X11; Linux x86_64) Gecko Firefox/5.0                # FF 5
Mozilla/5.0 (Windows; U; MSIE 9.0; Windows NT 9.0; en-US)        # IE 9
Opera/9.80 (Windows NT 5.1; U; en) Presto/2.8.119 Version/11.10  # Opera 11.10

For a detailed list, refer to onlinedatabases.

有关详细列表,请参阅在线数据库

回答by Perception

That isthe user agent string for Chrome. It has Mozilla in the name for historical reasons. Even funnier is when you see Mozilla in the user agent string for Internet Explorer :)

Chrome 的用户代理字符串。由于历史原因,它以 Mozilla 为名。更有趣的是,当您在 Internet Explorer 的用户代理字符串中看到 Mozilla 时:)

This link has UA strings for Chrome, you might find it useful: http://www.useragentstring.com/pages/Chrome/.

此链接包含 Chrome 的 UA 字符串,您可能会发现它很有用:http: //www.useragentstring.com/pages/Chrome/