Thread: Ipad detection

    #1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Mar 2008
    Posts
    670
    Rep Power
    38

    Ipad detection


    OK, I have a JavaScript sniffer script to dtect iPads and iPhones. The code looks like this::
    Code:
    	
    <script language="javascript">
    if (navigator.userAgent && ( navigator.userAgent.indexOf("Pad") > -1 || navigator.userAgent.indexOf("iPhone") > -1 ))
    {
    window.location.replace("systems_architecture_and_engineering_iPad.asp");
    }
    </script>
    What I need to do now, is that if someone goes to the "_iPad.asp" page and they are not on an ipad or iphone, it will redirect them to the regular page.

    So, I placed a sniffer in the "_iPad.asp" page, that should redirect to the regular page, if the view isn't on an iPad or iPhone. The code, which doesn't work, looks like this:
    Code:
    <script language="javascript">
    if (navigator.userAgent && ( navigator.userAgent.indexOf("Pad") < -1 || navigator.userAgent.indexOf("iPhone") < -1 ))
    {
    window.location.replace("systems_engineering_and_integration.asp");
    }
    </script>
    I just don't want people viewing the iPad page on a desktop computer. So, if by some chance they're on a desktop, and pull up the "_iPad.asp" page, they should be redirected to the regular page.

    Can anyone tell me the correct syntax for the 2nd bit of code?

    thanks!
    Last edited by lelales; December 12th, 2012 at 11:54 AM. Reason: typo
  2. #2
  3. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    indexOf returns -1 exactly when the value isn't found; you're comparing it to < -1, but -1 is not less than -1, so the condition is false.

    I'm assuming you know this already, but someone could view the ipad page on a desktop just by changing their user agent.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Mar 2008
    Posts
    670
    Rep Power
    38

    thanks E-Oreo


    I did not know the return value, so I was just guessing. This should be helpful.
    The thing I am doing is that, if you are not on and iPad and you go to the iPad version of the page, you will be redirected to the non ipad version.

    And vice versa. If you are on an ipad, and go to the non iPad version, you will be redirected tot he iPad version.

    A little convoluted, but this seemed to be the best way to go about it.

    thanks for your help!!

IMN logo majestic logo threadwatch logo seochat tools logo