Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Browser detection in Javascript --- ERROR?

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)
like image 447
DarkLightA Avatar asked Dec 23 '10 22:12

DarkLightA


People also ask

How do I find JavaScript errors?

Go to the screen where you are experiencing the error. In Chrome, navigate to View > Developer > JavaScript Console or More Tools > JavaScript Console or press Ctrl + Shift + J. The error console will open. If you don't see any errors try reloading the page.

How do I see JavaScript errors in Chrome?

In Chrome, navigate to Tools > Advanced > Error Console. The error console will open. Select JavaScript and Errors from the two drop downs. To find the error location, expand one of the errors.


1 Answers

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

like image 134
T.J. Crowder Avatar answered Oct 30 '22 10:10

T.J. Crowder