Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

is there a script to detect, if the visitor use iphone (whatever it's browser, may iphone Safari, iPhone for Opera or etc.)?

Then will shutdown some some of my JavaScript.

Thanks...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
249 views
Welcome To Ask or Share your Answers For Others

1 Answer

searching on the net there are two common ways of achieving this. My favorite though is in PHP its just so clean? wow. :D

In PHP you can write

<?php

function isIphone($user_agent=NULL) {
    if(!isset($user_agent)) {
        $user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
    }
    return (strpos($user_agent, 'iPhone') !== FALSE);
}

if(isIphone()) {
    header('Location: http://www.yourwebsite.com/phone');
    exit();
}

// ...THE REST OF YOUR CODE HERE

?>

and in javascript you can write

var agent = navigator.userAgent;
var isIphone = ((agent.indexOf('iPhone') != -1) || (agent.indexOf('iPod') != -1)) ;
if (isIphone) {
    window.location.href = 'http://www.yourwebsite.com/phone';
}

Hope that helps.

PK


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...