我有一个基于移动设备操作系统的重定向。 我尝试在以下脚本中返回重定向
$(document).ready(function (){
//Android Version:
if(navigator.userAgent.toLowerCase().indexOf("android") != -1) {
window.location.href = "https://link1";
}
if(navigator.userAgent.toLowerCase().indexOf("iphone") != -1){
window.location.href = 'https://link2';
}
if(navigator.userAgent.toLowerCase().indexOf("ipad") != -1){
window.location.href = 'https://link3';
}
});
如果以上都不成立,我需要第四次重定向。但是当我在脚本末尾使用时:
else {
window.location.href = 'https://link4';
}
只有 ipad 重定向有效。 Iphone 和 android 重定向到 link4。
最佳答案
每个 if
语句后面都需要一个 else if
,这样就可以代替 3 个单独的 if
语句,最后一个语句有一个else
,您有一个包含多个条件的 if
语句。
$(document).ready(function (){
//Android Version:
if(navigator.userAgent.toLowerCase().indexOf("android") != -1) {
window.location.href = "https://link1";
} else if(navigator.userAgent.toLowerCase().indexOf("iphone") != -1){
window.location.href = 'https://link2';
} else if(navigator.userAgent.toLowerCase().indexOf("ipad") != -1){
window.location.href = 'https://link3';
} else {
window.location.href = 'https://link4';
}
});
关于javascript - 三个 .indexOf 不匹配 - 返回另一个结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59238593/