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 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Jun 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    user agent mobile javascript question

    I know no javascript, so this is a basic question.

    <script type="text/javascript"> // <![CDATA[
    if ( (navigator.userAgent.indexOf('Android') != -1) ) {
    document.location = "mobileandroid.php";
    } // ]]>
    </script>

    <script language=javascript>

    if ((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
    location.replace("mobileios.php");
    }

    </script>

    How do i write the code for "else if" (all the users who are not using ios/android, including pc users) as in else if all visitors go to http//www.anothersite.com

    thank you for helping me out with this basic question.

  • #2
    New Coder
    Join Date
    Jun 2005
    Location
    Blackpool. UK
    Posts
    98
    Thanks
    0
    Thanked 4 Times in 4 Posts
    And your need for "browser sniffing" is?

    However use a switch() rather than a lot of if if ... else if .... else statements.
    Chris

    Indifference will be the downfall of mankind, but who cares?

  • #3
    New to the CF scene
    Join Date
    Jun 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    well i have a website that if users are using an android phone, i'd like them to go to one page, an iphone, go to anther page, or for others (including pc), go to a third page.

    so what would be the code for this using switch? i have no clue about javascript.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,313
    Thanks
    203
    Thanked 2,564 Times in 2,542 Posts
    Code:
    <script type = "text/javascript">
    
    if ((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
    location.replace("mobileios.php");
     }
    else {
    location.replace("http//www.anothersite.com");
    }
    
    
     </script>
    No point in using a switch statement with only two outcomes.

    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
    Last edited by Philip M; 06-05-2014 at 07:43 PM.

    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
    New to the CF scene
    Join Date
    Jun 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    but i need a third outcome: else if

    so what is the code for this, please?

    thanks?

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,313
    Thanks
    203
    Thanked 2,564 Times in 2,542 Posts
    Code:
    <script type = "text/javascript">
    
    if ((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
    location.replace("mobileios.php");
     }
    else if (/// whatever the third option is) {
    location.replace("http//www.somesite.com"); 
    }
    else {
    location.replace("http//www.anothersite.com");
    }
    
    
     </script>
    If that is unclear or does not meet your need, please spell out what the three outcomes are.
    Last edited by Philip M; 06-05-2014 at 07:51 PM.

    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.

  • #7
    New to the CF scene
    Join Date
    Jun 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ok great, thank you! this is what i needed!

  • #8
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Quote Originally Posted by Philip M View Post
    Code:
    <script type = "text/javascript">
    
    if ((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
    location.replace("mobileios.php");
     }
    else if (/// whatever the third option is) {
    location.replace("http//www.somesite.com"); 
    }
    else {
    location.replace("http//www.anothersite.com");
    }
    
    
     </script>
    If that is unclear or does not meet your need, please spell out what the three outcomes are.
    You forgot the check for Android.

    In this particular case, it would be better to do the browser sniffing in PHP not JS.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #9
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,313
    Thanks
    203
    Thanked 2,564 Times in 2,542 Posts
    Quote Originally Posted by glenngv View Post
    You forgot the check for Android.

    In this particular case, it would be better to do the browser sniffing in PHP not JS.
    Oh - I thought that was already covered.

    <script type="text/javascript"> // <![CDATA[
    if ( (navigator.userAgent.indexOf('Android') != -1) ) {
    document.location = "mobileandroid.php";
    } // ]]>
    </script>

    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.


  •  

    Posting Permissions

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