SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    SitePoint Addict
    Join Date
    Aug 2008
    Posts
    252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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.

    Code:
    <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

  2. #2
    Barefoot on the Moon! silver trophy Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,606
    Mentioned
    56 Post(s)
    Tagged
    1 Thread(s)
    Are you sure you're not opening Firefox? Firefox will show as "Mozilla". IE should only show as "Internet Explorer".
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain

  3. #3
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    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' )
    http://www.comptechdoc.org/independe...navigator.html



    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.
    http://www.w3schools.com/js/tryit.as...me=try_nav_all

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,813
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Iconic_creator View Post
    This is what I don't understand. When I open Internet Explorer, the browser name still say Mozilla.
    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.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    SitePoint Addict bronze trophy AllanP's Avatar
    Join Date
    Sep 2010
    Location
    Australia
    Posts
    298
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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

  6. #6
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    JimmyP came up with a nice idea for detecting IE and its versions: http://james.padolsey.com/javascript...onal-comments/

  7. #7
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,813
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by AllanP View Post
    Opera allows you to set the user agent to Opera, Firefox or IE.
    Safari does the same, plus Safari
    Safari also has a setting allowing you to change the useragent to anything at all.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  8. #8
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,813
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Raffles View Post
    JimmyP came up with a nice idea for detecting IE and its versions: http://james.padolsey.com/javascript...onal-comments/
    There's an even more certain way to detect IE and that is using JScript conditional comments.

    Code:
    /*@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
    @*/
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  9. #9
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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.

  10. #10
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,813
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Raffles View Post
    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.
    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.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  11. #11
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Yes, that's a good idea.

  12. #12
    Follow: @AlexDawsonUK silver trophybronze trophy AlexDawson's Avatar
    Join Date
    Feb 2009
    Location
    England, UK
    Posts
    8,111
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Raffles View Post
    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.
    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.

  13. #13
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,705
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Raffles View Post
    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.
    That particular script doesn't, which is where you might want to look to extend it.
    http://www.javascriptkit.com/javatut...lcompile.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.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  14. #14
    Non-Member bronze trophy
    Join Date
    Nov 2009
    Location
    Keene, NH
    Posts
    3,760
    Mentioned
    23 Post(s)
    Tagged
    0 Thread(s)
    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.

  15. #15
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,813
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by deathshadow60 View Post
    Pfft. Browser sniffing, what is this 1997?
    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.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •