无法使用JavaScript重定向到iPhone、iPad

无法使用JavaScript重定向到iPhone、iPad,javascript,android,ios,iphone,Javascript,Android,Ios,Iphone,我需要重新引导移动用户使用各自的设备,如iPhone、iPad或Android 对于Android,下面的javascript代码会正确重定向它 if ((navigator.userAgent.indexOf('Android') != -1)) { document.location = "http://www.cnn.com"; } 但我尝试了以下iPhone代码: if ((navigator.userAgent.indexOf('/iPhone/i') != -1) || (na

我需要重新引导移动用户使用各自的设备,如iPhone、iPad或Android

对于Android,下面的javascript代码会正确重定向它

if ((navigator.userAgent.indexOf('Android') != -1)) {
  document.location = "http://www.cnn.com";
}
但我尝试了以下iPhone代码:

if ((navigator.userAgent.indexOf('/iPhone/i') != -1) || (navigator.userAgent.indexOf('/iPod/i') != -1)) {
  document.location = "http://www.ebay.com";
}

以上代码都不会在iPhone或iPad中重定向网站

我不明白为什么


请提供帮助或建议。

我已经能够使用此Javascript在Android、iOS和BB中获得重定向

<html>
<script type='text/javascript'>

function detectDevice()
{
    var ua = navigator.userAgent;
    if (ua.indexOf("BlackBerry") >= 0) {
        window.location.replace("http://www.cnn.com");
    }
    else if (ua.indexOf("iPhone") >= 0){
        window.location.replace("http://www.ebay.com");
    }
    else if (ua.indexOf("iPad") >= 0){
        window.location.replace("http://www.ebay.com");
    }
    else if (ua.indexOf("Android") >= 0){
        window.location.replace("http://www.google.com");
    }
}


</script>
<body onload="detectDevice()">
</body>
</html>

函数检测设备()
{
var ua=navigator.userAgent;
如果(ua.indexOf(“BlackBerry”)>=0){
window.location.replace(“http://www.cnn.com");
}
如果(ua.indexOf(“iPhone”)>=0,则为else{
window.location.replace(“http://www.ebay.com");
}
如果(ua.indexOf(“iPad”)>=0,则为其他情况){
window.location.replace(“http://www.ebay.com");
}
如果(ua.indexOf(“安卓”)>=0,则为else{
window.location.replace(“http://www.google.com");
}
}

有一次我看到奇怪的bug,当设置
位置时
直接导致错误。作为猜测,我建议您编写
document.location.href=”http://www.ebay.com";,但最好调试代码。至少使用
alert
s和
try/catch
。这不是问题所在,但您可以轻松使用PHP
header('位置:http://google.com');否,它在iPhone中仍然不会重定向。
<html>
<script type='text/javascript'>

function detectDevice()
{
    var ua = navigator.userAgent;
    if (ua.indexOf("BlackBerry") >= 0) {
        window.location.replace("http://www.cnn.com");
    }
    else if (ua.indexOf("iPhone") >= 0){
        window.location.replace("http://www.ebay.com");
    }
    else if (ua.indexOf("iPad") >= 0){
        window.location.replace("http://www.ebay.com");
    }
    else if (ua.indexOf("Android") >= 0){
        window.location.replace("http://www.google.com");
    }
}


</script>
<body onload="detectDevice()">
</body>
</html>