Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute a javaScript function only when using an IPhone / IPad

I want to execute a javascript function, only if the user is using an IPad / IPhone.

Something like this:

var checkifipadoriphone = true; //or false
if(checkifipadoriphone){
    executesomefuntion();
}

How does one do this?

Thanks!

like image 550
James Cazzetta Avatar asked Dec 30 '25 01:12

James Cazzetta


2 Answers

function isiPhone(){
    return (
        //Detect iPhone
    //var isiPad = navigator.userAgent.match(/iPad/i) != null;
        (navigator.platform.indexOf("iPhone") != -1) ||
        //Detect iPod
        (navigator.platform.indexOf("iPad") != -1)
    );
}

if(isiPhone()){
    executesomefuntion();
}
like image 71
James Cazzetta Avatar answered Dec 31 '25 15:12

James Cazzetta


Check out this page to see if you can use it to extract the browser type from the navigator object.

like image 22
bdparrish Avatar answered Dec 31 '25 13:12

bdparrish