SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Wizard jag5311's Avatar
    Join Date
    Jan 2003
    Location
    Somewhere in Indiana
    Posts
    3,082
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Browser Sniffing not quite working as planned

    Hi, I have some code I am working off of that is determining the user agent string to determine which browser a user is using.

    Here is that code

    Code:
     var ver = navigator.appName;
     var agt = navigator.userAgent.toLowerCase();
     var num = parseInt(navigator.appVersion);
     var msg2 = "";
     var link = "";
     
     /* Determine OS */
     var MAC = (agt.indexOf("mac")!=-1);
     var WIN = (agt.indexOf("windows") != -1); if(!MAC && !WIN) { var OTHER_OS = true; } else { var OTHER_OS = false; }
     
     
     /* Determine Browser */
     var NS   = (agt.indexOf("netscape")!=-1);
     var IE   = (agt.indexOf("msie")!=-1);
     var IE7  = (agt.indexOf("msie 7.")!=-1);
     var FF   = (agt.indexOf("firefox")!=-1);
     var OP   = (agt.indexOf("opera")!=-1); 
     var OP_IE = ((agt.indexOf("msie") != -1) || (agt.indexOf("opera") == -1));
     if(!NS && !IE && !FF && !OP && !IE7) { var OTHER_BROWSER = true; } else { var OTHER_BROWSER = false; }
    Then my output code

    Code:
     				if (IE) { 
     					msg2 =  "This is IE"; 
      					document.write(msg2);
     				}
     				if (FF) {
     					msg2 =  "This is Firefox"; 
     					document.write(msg2);
     				}
     				if (OP) {
     					msg2 =  "This is Opera; 
      					document.write(msg2);
     				}
     				if (NS) {
     					msg2 =  "This is Netscape; 
      					document.write(msg2);
     				}
    Now, Firefox and IE get there proper messages, but OPERA is acting like IE, and netscape is combining the msg2 content from OP AND NS.

    why?

    Thanks,
    Bryan

  2. #2
    SitePoint Zealot Beeper's Avatar
    Join Date
    Sep 2003
    Location
    LONDON UK
    Posts
    199
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is not a problem with your script but rather an odd quirk of the Opera default settings...

    If you go into tools>preferences and select the advanced tab then select 'Network' from left hand menu and change 'Browser Identification' to "Identify as Opera" then you will probably find your script detects perfectly well.

    However this doesn't help your browser detection as the default for Opera is "Identify as MSIE 6.0" so most Opera users will still return "Explorer". This begs the question "Why do you care what browser they are using?". Mostly the answer has to do with scripting for getElementByID and therefore the best method is not to detect what browser it is but what capabilities it has.

    Hope that helps
    Never argue with an idiot.
    They just drag you down to their level...
    and beat you with experience.

  3. #3
    SitePoint Wizard jag5311's Avatar
    Join Date
    Jan 2003
    Location
    Somewhere in Indiana
    Posts
    3,082
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks. Yea, we are offering up a toolbar downoad based on their browser.

    Funny thing is, Google and Ask.com both offer up their IE download of their toolbar to Opera users, even though Opera most likely can't use it

    I just wanted to go the extra step and NOT give opera the download, as ours is only for Firefox and IE.

    Thanks

  4. #4
    SitePoint Zealot Beeper's Avatar
    Join Date
    Sep 2003
    Location
    LONDON UK
    Posts
    199
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I guess a way around it would be to check any browser that responds as IE to see if it can execute JavaScript that IE can't. If it can then it is probably Opera. I am not brilliant but the below code might help. Basically I test any MSIE return for window.opera which will return false unless it is actually opera and exclude returns that test positive for window.opera. To test for Opera I check to see that window.opera is not false. It might need some debugging but I think it might work.

    Code:
    /* Determine Browser */
     var NS   = (agt.indexOf("netscape")!=-1);
     var IE   = ((!window.opera) && (navigator.userAgent.indexOf("ie") != -1));
     var IE7  = (agt.indexOf("msie 7.")!=-1);
     var FF   = (agt.indexOf("firefox")!=-1);
     var OP   = (window.opera !=-1); 
     if(!NS && !IE && !FF && !OP && !IE7) { var OTHER_BROWSER = true; } else { var OTHER_BROWSER = false; }
    Never argue with an idiot.
    They just drag you down to their level...
    and beat you with experience.

  5. #5
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    The problem is the order in which you are testing for the different browsers. Most browsers can be set so that their useragent reports them as IE but in most cases the real browser is also identified elsewhere in the useragent.

    See http://www.quirksmode.org/js/detect.html for a browser detection script that gets it right most of the time.
    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
  •