Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Help - Using Objects with If/then Statements

    Hi, I am trying to detect a users browsers and output a different code based on browser.

    I found this code online that uses the object detection method and it works. When I document.write(BrowserDetect.browser) it outputs Chrome, (which is what I am using). The code can be found here: http://www.quirksmode.org/js/detect.html

    So I can't understand why the following code does not work? It ouputs the "not" in the else statement.

    I believe that BrowserDetect is an object and the .browser at the end is a property of the object.

    Can one not make an object a variable? or make an object a string?

    oh and BTW there is a bunch of javascript that creates the browserdetect object that can be found on the site above, but I thought it would be too unwieldy to post here. Thanks.

    The following script outputs:
    Chrome Not Working

    <script type="text/javascript">

    document.write(BrowserDetect.browser);

    var browser="BrowserDetect.browser";

    if(browser=="Chrome")
    {
    document.write(" Working");
    }
    else
    {
    document.write(" Not working");
    }
    </script>
    Last edited by daled1; 09-26-2011 at 07:42 AM.

  • #2
    Regular Coder
    Join Date
    Sep 2011
    Location
    Sweden
    Posts
    154
    Thanks
    1
    Thanked 22 Times in 22 Posts
    It doesn't work because you are not calling the BrowserDetect object asking for its property browser, instead you are just assigning "BrowserDetect.browser" as a string value to the variable browser. Remove the quotation marks and it should work:
    Code:
    var browser = BrowserDetect.browser;
    
    if(browser=="Chrome")
    {
    document.write("<b>Good morning</b>");
    }
    else
    {
    document.write("<b>not</b>");
    }
    (Also be sure to include PPK:s detection code before your own code...)

  • Users who have thanked ironboy for this post:

    daled1 (09-26-2011)

  • #3
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Ah...right. Thanks it works!

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,016
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    To detect Chrome:-

    Code:
    var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') >-1;
    alert (is_chrome);
    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    You are dealing with a rather academical problem. Usually no one needs to detect browsers; the coders need, most of the time, to detect if a browser has implemented a method or another, thus usually they detect the existence of that method, not of the browser's type.

    After all, the browser's type is, most of the time, irrelevant. Same as the color of a cat, as long as she catches mice
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

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