JavaScript Browser Dectection

Hello everyone,

I’m learning to write some Javascript. I wrote this script to detect browsers, it’s a popular method.

I do not plan to do anything with this and may never use, it’s just for learning purposes.

This is what I don’t understand. When I open Internet Explorer, the browser name still say Mozilla.

Why is this happening. Again this is not for real world use.


<script type="text/javascript">
document.write("Browser Name: " + navigator.appCodeName);
document.write("<br />");
document.write("Browser Version: " + navigator.appVersion);
document.write("<br />");
document.write("Java Enabled: " + navigator.javaEnabled());
document.write("<br />");
document.write("Cookies Enabled: " + navigator.cookieEnabled);
document.write("<br />");
document.write("Browser Type: " + navigator.appName);
</script>

IC

No and in 1997 the comment would have been “Pfft. Browser sniffing, what is this 1897?”. Browser sniffing was definitely looked down on as being extremely poor practice and unnecessary long before 1997. In 1997 the main three browsers to support JavaScript were Netscape 2, Netscape 3, and IE3 and feature sensing was the recommended way of determining whether the new JavaScript commands introduced in Netscape 3 would work or not.

That particular script doesn’t, which is where you might want to look to extend it.
http://www.javascriptkit.com/javatutors/conditionalcompile.shtml

It’s futile to look for browser versions though. Browsers always change, and you don’t want to change your code all the time to match up with them.

Microsoft did that deliberately as many web pages that actually did such tests were checking if the browser was Netscape 3 and so in order to fool the code into accepting IE3 as well they set the useragent to the same value as Netscape. It is because people used those fields to identify certain browsers and reject others that all browsers used the same values for the field as other browsers that were accepted. That’s why all browsers have the abiliity to fill that field with the same value as recent versions of IE and Netscape use.

Opera can change the field values automatically depending on exactly what web page is being visited and so tell pages that only work on IE that it is IE and pages that only work on Firefox that it is Firefox.

In the case of Internet Explorer, Firefox, and Safari you can set the useragent to anything you like and so when the useragent says “Eat At Joe’s” it is most likely one of those three browsers.

Are you sure you’re not mixing appName and appCodeName up?

navigator.appName should give the name of the browser, while navigator.appCodeName should give what it thinks it is (for some strange reason IE thinks it’s Mozilla-compatible, hah!)

Here’s more info (found on Google by typing ‘javascript navigator’ :slight_smile: )

Edit>>
OK, I just found the page where you probably got that script from and apparently my Firefox 3.6.10 browser thinks it’s Netscape 5. :smiley:

Being fair though, the JScript versions do have some synthesis with their version of the browser. JScript 9 = IE9, 5.8 = IE8, 5.7 = IE7, 5.6 = IE6, 5.5 = IE5.5. :slight_smile:

JimmyP came up with a nice idea for detecting IE and its versions: http://james.padolsey.com/javascript/detect-ie-in-js-using-conditional-comments/

Here is a page that does the same thing, but slightly differently.
http://www.grafwebdesign.com/version1.htm

Try running it in different browsers to see the result.
Opera allows you to set the user agent to Opera, Firefox or IE.
Safari does the same, plus Safari

Yes, that’s a good idea.

Yes, but that doesn’t tell you the version of the browser. I know it could tell you the JScript version, but that’s not the same.

There’s an even more certain way to detect IE and that is using JScript conditional comments.


/*@cc_on
  @if (@_jscript)
alert('This browser is running JScript and is therefore Internet Explorer');
  @else */
alert('This browser is running JavaScript and is therefore NOT Internet Explorer');   /*
  @end
@*/

Pfft. Browser sniffing, what is this 1997?

Browser sniffing is most always made of total /FAIL/ – you should be detecting capabilities as needed, not the browser.

It would still be more efficient to wrap the code that tests for what version of IE that it is inside the jscript conditional comment so that it doesn’t even try to run for other browsers which don’t have an IE version number.

Safari also has a setting allowing you to change the useragent to anything at all.

Are you sure you’re not opening Firefox? Firefox will show as “Mozilla”. IE should only show as “Internet Explorer”.