Javascript 中的浏览器检测 --- 错误?
这是检测器: 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许应该,这取决于他们想要实现的目标。
但这只是说明了为什么浏览器检测已经声名狼藉,而有利于功能检测。浏览器检测代码的老化速度令人难以置信很快。另一方面,特征检测是相当永恒的。
例如:我可以检查浏览器是否是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.)
首先,我发现 Quirksmode BrowserDetect 脚本 是一个真正的宝石,并且可能会比你现在使用的更好。
维基百科解释用户代理字符串几乎总是以“Mozilla”开头:
但是,为了向您解释为什么所有浏览器都声称自己是“Mozilla”,您必须稍微回顾一下 20 世纪 90 年代的浏览器战争...... org/blog/user-agent-string-history/" rel="nofollow">webaim.org 上的这篇文章:
您应该阅读整篇文章,它内容丰富且有趣。但长和排序是,您需要解析用户代理字符串,因为它们几乎都说“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":
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:
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"