【问题标题】:Redirecting to app store using window.location使用 window.location 重定向到应用商店
【发布时间】:2016-07-21 19:09:45
【问题描述】:

我有一个脚本,用于检查以确保我在移动设备上,获取包含应用列表及其应用商店地址的提要,然后根据设备类型调用一个函数,将用户重定向到正确的应用页面。

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.locationwindow.location.hrefwindow.location.assign(url)window.location.replace(url),但都没有工作。任何想法为什么这不起作用?

【问题讨论】:

  • 你看过url值吗? console.log(url).
  • 是的,URL 值是一个合适的 itunes.apple.com... 链接。当我直接转到链接时,链接确实会弹出。

标签: javascript redirect window


【解决方案1】:

我用这个例子改造了你的功能并为我工作得很好:

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 试过了。

【讨论】:

  • 链接(参见上面的问题 cmets)是具有适当协议的绝对 URL。
猜你喜欢
  • 1970-01-01
  • 2016-05-27
  • 2015-09-24
  • 1970-01-01
  • 2022-01-17
  • 2017-03-07
  • 1970-01-01
  • 2014-04-06
  • 1970-01-01
相关资源
最近更新 更多