开发者

Browser detection in Javascript --- ERROR?

开发者 https://www.devze.com 2023-01-31 20:29 出处:网络
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.

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: " 开发者_StackOverflow+ 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)


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.)


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"

0

精彩评论

暂无评论...
验证码 换一张
取 消