我需要将移动用户重定向到使用相应的设备,即 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>
关于javascript - 无法使用 JavaScript 重定向到 iPhone、iPad,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23920307/