我有一个脚本,用于检查以确保我使用的是移动设备,获取包含应用程序列表及其应用程序商店地址的提要,然后调用一个函数,该函数取决于设备的类型,将用户重定向到正确的应用页面。
var redirecToAppPage = function(device, types) {
var url;
if (device === 'android') {
url = types.android;
} else if (device === 'iphone' || device === 'ipod') {
url = types.apple;
} else if (device === 'ipad') {
url = types.apple;
} else if (device === 'blackberry') {
url = types.phone;
}
if (url) {
window.location = url;
return false;
}
return url;
};
这不起作用。重定向永远不会发生,而是执行页面上的其余代码。
我尝试过 window.location
、window.location.href
、window.location.assign(url)
和 window.location.replace(url)
,它们都不起作用。有什么想法为什么这行不通吗?
最佳答案
我通过这个例子 reshape 了你的功能并且对我来说效果很好:
function redirectToAppPage(device, types) {
var url;
if (device === 'android') {
url = types.android;
} else if (device === 'iphone' || device === 'ipod') {
url = types.apple;
} else if (device === 'ipad') {
url = types.apple;
} else if (device === 'blackberry') {
url = types.phone;
}
if (url) {
window.location.replace(url);
}
}
// Example
var device = 'android';
var types = {
android : "https://www.apple.com/itunes/"
}
redirectToAppPage(device, types);
用jsfiddle尝试过。
关于javascript - 使用 window.location 重定向到应用程序商店,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38512531/