7

我正在开发网站的移动版本。我目前正在使用这个 Javascript 来检测和重定向用户:

if((navigator.userAgent.match(/iPhone/i)) || 
                (navigator.userAgent.match(/Android/i)) ||
                (navigator.userAgent.match(/iPod/i))) 
        { 
        window.location = "http://sitename.com/m/";
    }

适用于 iPhone 和 iPod,但不适用于 Android。我在 Eclipse 中使用 Android 模拟器。我没有实际测试它的 Android 小工具。

难道我做错了什么?有人有同样的问题吗?

4

2 回答 2

12

您应该使用location.replace而不是window.location

示例:

if( (navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/Android/i)) || (navigator.userAgent.match(/iPod/i)) ) { 
    location.replace("http://sitename.com/m/");
}

我使用了这段代码,它适用于 iphone/itouch 和 android 手机/设备。

于 2010-02-16T17:00:11.353 回答
3

这是我的 JavaScript 函数来检测 Android 设备:

function isAndroid() {
    var ua = navigator.userAgent;
    return ua.match(/Android/) 
        || ua.match(/Dalvik/)
        || ua.match(/GINGERBREAD/)
        || ua.match(/Linux;.*Mobile Safari/)
        || ua.match(/Linux 1\..*AppleWebKit/)
};
于 2011-11-09T15:38:31.613 回答