8

If a user surfs to my site I want to display a message if he using Android phone. My website is http://website.com

How can I do this?

I have tried this but not succeeded:

var ua = navigator.userAgent.toLowerCase();
var isAndroid = ua.indexOf("android") > -1; //&& ua.indexOf("mobile");
if(isAndroid) {
    // Do something!
    // Redirect to Android-site?
    window.location = 'http://android.davidwalsh.name';
}
4

3 回答 3

30

您还可以使用 PHP 和 Javascript 检测 Android 用户代理:

使用 PHP:

$ua = strtolower($_SERVER['HTTP_USER_AGENT']);
if(stripos($ua,'android') !== false) { // && stripos($ua,'mobile') !== false) {
    echo "your message";
}

使用 JavaScript:

var ua = navigator.userAgent.toLowerCase();
var isAndroid = ua.indexOf("android") > -1; //&& ua.indexOf("mobile");
if(isAndroid) {
    // Do something!
    alert('Your message');
}
于 2013-10-17T12:16:34.277 回答
2

对于 PHP:

$isAndroid = (strpos($_SERVER['HTTP_USER_AGENT'], 'Android') !== false);

于 2014-10-08T06:42:08.947 回答
0
function isAndroid() {
    if(stripos($_SERVER['HTTP_USER_AGENT'],'android') !== false) { 
        return true;
    }
    return false;
}

用法:

if(isAndroid()) {
    echo "an android device";
} else {
    echo "non-android device";
}
于 2021-12-12T11:44:22.577 回答