5

I am looking for a way to detect if a page is visited by an iPhone.

What I'm basically looking for is for a way to stop all but iPhone from viewing a specific web page.

Something like...

If Browser !=iPhone then exit;

Is this possible using Javascript?

3
  • Can I ask why? (My Android isn't in the cool kids club?) Many years ago, we tried allowing only certain browsers to view our pages, and we've since decided that was a terrible idea. It's why--to this day--IE claims it's Mozilla. Don't detect browser; feature detect. Commented Aug 5, 2012 at 22:50
  • No special reason really Commented Aug 6, 2012 at 9:34
  • For example a page with links to apple store Commented Aug 6, 2012 at 12:04

2 Answers 2

15
if (navigator.userAgent.toLowerCase().indexOf("iphone") ==-1) 
  location.replace("goaway.html");
Sign up to request clarification or add additional context in comments.

3 Comments

And if it's a jail broken iPhone running a browser other than Safari?
And what? Or a PC with safari with a fake userAgent. Or anything with JS turned off
Precisely, so do it at the server, or just put a "iPhone users click here" button on the site.
3
if(navigator.userAgent.match(/iPhone/i)) {
   ...
}

Though i would recommend you to do that before the DOM is loaded, e.g. with PHP:

<?php
  if(strstr($_SERVER['HTTP_USER_AGENT'],'iPhone')) {

  }
?>

1 Comment

Good answer - however there is no need to match, merely to test for the existence of the iPhone string, so indexOf() is better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.