Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript to detect iPhone web browser

Tags:

javascript

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?

like image 463
Satch3000 Avatar asked Dec 03 '22 02:12

Satch3000


2 Answers

if (navigator.userAgent.toLowerCase().indexOf("iphone") ==-1) 
  location.replace("goaway.html");
like image 126
mplungjan Avatar answered Dec 18 '22 13:12

mplungjan


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

  }
?>
like image 35
Marcel Kalveram Avatar answered Dec 18 '22 15:12

Marcel Kalveram