Javascript 中的浏览器检测 --- 错误?

发布于 2024-10-09 08:27:00 字数 2296 浏览 7 评论 0原文

这是检测器: http://w3schools.com/js/tryit.asp?filename= try_nav_all

在 Chrome、Firefox、Safari Netscape 中,它始终将浏览器代号显示为 Mozilla,将浏览器名称显示为 Netscape。这不应该根据浏览器而改变吗?

如果您有兴趣,这里是代码和不同的输出:

代码:

document.write("Browser CodeName: " + navigator.appCodeName);
document.write("<br /><br />");
document.write("Browser Name: " + navigator.appName);
document.write("<br /><br />");
document.write("Browser Version: " + navigator.appVersion);
document.write("<br /><br />");
document.write("Cookies Enabled: " + navigator.cookieEnabled);
document.write("<br /><br />");
document.write("Platform: " + navigator.platform);
document.write("<br /><br />");
document.write("User-agent header: " + navigator.userAgent);

CHROME 输出:

Browser CodeName: Mozilla

Browser Name: Netscape

Browser Version: 5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.224 Safari/534.10

Cookies Enabled: true

Platform: Win32

User-agent header: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.224 Safari/534.10

FIREFOX 输出:

Browser CodeName: Mozilla

Browser Name: Netscape

Browser Version: 5.0 (Windows; es-ES)

Cookies Enabled: true

Platform: Win32

User-agent header: Mozilla/5.0 (Windows; U; Windows NT 6.1; es-ES; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12 

SAFARI 输出:

Browser CodeName: Mozilla

Browser Name: Netscape

Browser Version: 5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.19.4 (KHTML, like Gecko) Version/5.0.3 Safari/533.19.4

Cookies Enabled: true

Platform: Win32

User-agent header: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.19.4 (KHTML, like Gecko) Version/5.0.3 Safari/533.19.4

NETSCAPE 输出:

Browser CodeName: Mozilla

Browser Name: Netscape

Browser Version: 5.0 (Windows; en-US)

Cookies Enabled: true

Platform: Win32

User-agent header: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.7.2) Gecko/20040804 Netscape/7.2 (ax)

This is the detector: http://w3schools.com/js/tryit.asp?filename=try_nav_all

In Chrome, Firefox, Safari and Netscape it always shows the browser codename as Mozilla, and the browser name as Netscape. Shouldn't this change according to the browser?

Here is the code and the different outputs, if you're interested:

CODE:

document.write("Browser CodeName: " + navigator.appCodeName);
document.write("<br /><br />");
document.write("Browser Name: " + navigator.appName);
document.write("<br /><br />");
document.write("Browser Version: " + navigator.appVersion);
document.write("<br /><br />");
document.write("Cookies Enabled: " + navigator.cookieEnabled);
document.write("<br /><br />");
document.write("Platform: " + navigator.platform);
document.write("<br /><br />");
document.write("User-agent header: " + navigator.userAgent);

CHROME OUTPUT:

Browser CodeName: Mozilla

Browser Name: Netscape

Browser Version: 5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.224 Safari/534.10

Cookies Enabled: true

Platform: Win32

User-agent header: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.224 Safari/534.10

FIREFOX OUTPUT:

Browser CodeName: Mozilla

Browser Name: Netscape

Browser Version: 5.0 (Windows; es-ES)

Cookies Enabled: true

Platform: Win32

User-agent header: Mozilla/5.0 (Windows; U; Windows NT 6.1; es-ES; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12 

SAFARI OUTPUT:

Browser CodeName: Mozilla

Browser Name: Netscape

Browser Version: 5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.19.4 (KHTML, like Gecko) Version/5.0.3 Safari/533.19.4

Cookies Enabled: true

Platform: Win32

User-agent header: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.19.4 (KHTML, like Gecko) Version/5.0.3 Safari/533.19.4

NETSCAPE OUTPUT:

Browser CodeName: Mozilla

Browser Name: Netscape

Browser Version: 5.0 (Windows; en-US)

Cookies Enabled: true

Platform: Win32

User-agent header: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.7.2) Gecko/20040804 Netscape/7.2 (ax)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

身边 2024-10-16 08:27:00

也许应该,这取决于他们想要实现的目标。

但这只是说明了为什么浏览器检测已经声名狼藉,而有利于功能检测。浏览器检测代码的老化速度令人难以置信很快。另一方面,特征检测是相当永恒的。

例如:我可以检查浏览器是否是IE,如果是,则假设该浏览器没有Array.prototype.indexOf。但后来 IE9 出现并添加了它,但就像一个杯子一样,我仍然使用我自己的版本,因为我认为“IE”没有它。更好的是实际检查它是否存在于我正在运行的实现中,而不必太关心它是什么品牌。当然,功能检测可以与我从未听说过的浏览器一起使用;浏览器检测将失败并依赖于一些完全任意的“默认”。

有时它并不像执行 if (Array.prototype.indexOf) 那样直接,但通常是可能的。 Juriy Zaytsev (kangax) 有 一个很棒的功能检测列表

(旁注:几乎所有浏览器都声称(在某种程度上)它们是“Mozilla”,因为有些网站很容易在首选 Netscape 浏览器(是的,很久以前)上使用 Microsoft 浏览器,并且正在这样做浏览器检测来检查。)

Perhaps it should, it depends on what they're trying to achieve.

But it just demonstrates why browser detection has fallen into disrepute, in favor of feature detection. Browser detection code ages incredibly quickly. Feature detection, on the other hand, is fairly timeless.

For example: I could check to see if the browser is IE and, if so, assume that the browser doesn't have Array.prototype.indexOf. But then IE9 comes along and adds it, but like a mug I'm still using my own version because I think "IE" doesn't have it. Much better to actually check to see if it exists in the implementation I'm running on, without much caring what brand it is. And of course, feature detection will work with a browser I've never heard of; browser detection will fail and fall back on some completely-arbirary "default".

Sometimes it's not as straight-forward as doing an if (Array.prototype.indexOf), but it's usually possible. Juriy Zaytsev (kangax) has a great list of feature detection stuff.

(Side note: Nearly all browsers claim (at some level) that they're "Mozilla", because some sites easly on preferred Netscape browsers (yes, that long ago) to Microsoft ones, and were doing browser detection to check.)

匿名。 2024-10-16 08:27:00

首先,我发现 Quirksmode BrowserDetect 脚本 是一个真正的宝石,并且可能会比你现在使用的更好。

维基百科解释用户代理字符串几乎总是以“Mozilla”开头:

基于上述内容,Web 浏览器使用的非官方格式如下:Mozilla/[版本]([系统和浏览器信息])[平台]([平台详细信息])[扩展名]。

但是,为了向您解释为什么所有浏览器都声称自己是“Mozilla”,您必须稍微回顾一下 20 世纪 90 年代的浏览器战争...... org/blog/user-agent-string-history/" rel="nofollow">webaim.org 上的这篇文章:

[...] 然后出现了一种新的网络浏览器,称为“Mozilla”,是“Mosaic Killer”的缩写,但 Mosaic 不高兴,因此公开名称改为 Netscape,Netscape 自称为 Mozilla/1.0(Win3.0)。 1)[...]

[...]微软变得不耐烦了,不想等待网站管理员了解IE并开始向其发送帧,因此Internet Explorer宣布它“与Mozilla兼容”并开始冒充Netscape,并自称Mozilla/1.22(兼容;MSIE 2.0;Windows 95),[...]

微软与 Windows 一起出售了 IE,并使其比 Netscape 更好,第一次浏览器战争在这片土地上爆发。瞧,网景被杀了,微软却欢欣鼓舞。但 Netscape 重生为 Mozilla,Mozilla 构建了 Gecko,并称自己为 Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.1) Gecko/20020826,Gecko 是渲染引擎,Gecko 很好。 Mozilla 成为 Firefox,并称自己为 Mozilla/5.0 (Windows; U; Windows NT 5.1; sv-SE; rv:1.7.5) Gecko/20041108 Firefox/1.0,Firefox 非常好。 Gecko 开始繁衍,其他使用其代码的浏览器也诞生了,他们称自己为 Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.7.2) Gecko/20040825 Camino /0.8.1 一个,Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.8.1.8) Gecko/20071008 SeaMonkey/1.0 另一个,每个都假装是 Mozilla,并且全部由 Gecko 提供支持。 [...]

您应该阅读整篇文章,它内容丰富且有趣。但长和排序是,您需要解析用户代理字符串,因为它们几乎都说“Mozilla / something

First, I find that the Quirksmode BrowserDetect script is a real gem, and is probably going to be better than what you're using now.

Wikipedia explains that User Agent strings almost always start with "Mozilla":

An unofficial format, based on the above, used by Web browsers is as follows: Mozilla/[version] ([system and browser information]) [platform] ([platform details]) [extensions].

But, to give you an explanation for why all browsers are claiming to be "Mozilla", you have to go back in time a bit to the browser wars of the 1990s... From this article on webaim.org:

[...] then came a new web browser known as “Mozilla”, being short for “Mosaic Killer,” but Mosaic was not amused, so the public name was changed to Netscape, and Netscape called itself Mozilla/1.0 (Win3.1) [...]

[...] And Microsoft grew impatient, and did not wish to wait for webmasters to learn of IE and begin to send it frames, and so Internet Explorer declared that it was “Mozilla compatible” and began to impersonate Netscape, and called itself Mozilla/1.22 (compatible; MSIE 2.0; Windows 95), [...]

And Microsoft sold IE with Windows, and made it better than Netscape, and the first browser war raged upon the face of the land. And behold, Netscape was killed, and there was much rejoicing at Microsoft. But Netscape was reborn as Mozilla, and Mozilla built Gecko, and called itself Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.1) Gecko/20020826, and Gecko was the rendering engine, and Gecko was good. And Mozilla became Firefox, and called itself Mozilla/5.0 (Windows; U; Windows NT 5.1; sv-SE; rv:1.7.5) Gecko/20041108 Firefox/1.0, and Firefox was very good. And Gecko began to multiply, and other browsers were born that used its code, and they called themselves Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.7.2) Gecko/20040825 Camino/0.8.1 the one, and Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.8.1.8) Gecko/20071008 SeaMonkey/1.0 another, each pretending to be Mozilla, and all of them powered by Gecko. [...]

You should read the whole article, it's informing and amusing. But the long and the sort is, you need to parse the User Agent string, as they pretty much all say "Mozilla / something"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文