Android模拟器浏览器检测

时间:2010-02-12 19:32:31

标签: android browser android-emulator

我正在开发一个网站的移动版本。我目前正在使用此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小工具来实际测试它。

我做错了吗?有人有同样的问题吗?

3 个答案:

答案 0 :(得分: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手机/设备。

答案 1 :(得分: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/)
};

答案 2 :(得分:0)

window.location在Android中不起作用?真?在Android 2.3.4上看起来对我来说效果很好。你们在没有得到window.location以在浏览器中成功加载新URL的情况下使用了哪些Android版本?

相关问题