James Cazzetta
James Cazzetta

Reputation: 3220

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!

Upvotes: 3

Views: 3767

Answers (2)

James Cazzetta
James Cazzetta

Reputation: 3220

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();
}

Upvotes: 3

bdparrish
bdparrish

Reputation: 2764

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

Upvotes: 2

Related Questions