繁体   English   中英

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

[英]Unable to redirect to iPhone, iPad using JavaScript

我需要将移动用户重定向到使用相应的设备,例如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";
}

要么

if ((navigator.userAgent.indexOf('iPhone') != -1) || (navigator.userAgent.indexOf('iPod') != -1) || (navigator.userAgent.indexOf('iPad') != -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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM