Satch3000
Satch3000

Reputation: 49422

Javascript to detect iPhone web browser

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?

Upvotes: 5

Views: 10920

Answers (2)

mplungjan
mplungjan

Reputation: 178375

if (navigator.userAgent.toLowerCase().indexOf("iphone") ==-1) 
  location.replace("goaway.html");

Upvotes: 15

Marcel Kalveram
Marcel Kalveram

Reputation: 1305

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')) {

  }
?>

Upvotes: 3

Related Questions