【问题标题】:pushstate/pop state issue on IOS - history api/javascriptIOS 上的 pushstate/pop 状态问题 - 历史 api/javascript
【发布时间】:2015-12-20 08:52:40
【问题描述】:

我只在 iphone 和 ipad 上遇到了一个奇怪的问题。我已经构建了以下功能:

在电子商务网站上,当用户在结帐成功页面上单击浏览器的后退按钮一次时,我会将他重定向到主页。这适用于所有主流浏览器。

但是,在IOS上,在点击返回按钮之前,页面会自动重定向到主页(在结帐成功页面上)

我有以下代码:

(function() {

var SuccessPage ={

    extractDomain:function(url){
        var domain;
        if (url.indexOf("://") > -1) {
            domain = url.split('/')[2];
        }
        else {
            domain = url.split('/')[0];
        }
        domain = domain.split(':')[0];

        return domain;
    }

}


if(window.location.href.indexOf("checkout/onepage/success") > -1) {
    history.pushState(null, null, window.location.href);
    window.addEventListener("popstate", function(e) {
        window.location.assign("http://"+SuccessPage.extractDomain(window.location.href))
    });
}

})();

我尝试在弹出状态事件上使用 set timeout.. 但不抽雪茄 :( 而且我无法弄清楚为什么 IOS 不理解这段代码..

请帮忙

【问题讨论】:

  • 我刚刚遇到了 history.js,对于我正在尝试做的简单任务,这是否值得实施,还是过于矫枉过正?
  • 根据我的发现,Safari 会在页面加载时触发 popstate 事件。

标签: javascript jquery html5-history


【解决方案1】:

搞定了:

    if(window.location.href.indexOf("checkout/onepage/success") > -1) {
    if(window.history && history.pushState && history.state !== undefined){
        history.pushState(null, null, window.location.href);
        window.addEventListener("popstate", function(event) {
            if(navigator.userAgent.match(/(iPod|iPhone|iPad)/)){
                if(!page_loaded){
                    page_loaded = true;
                    return false;
                }else{
                    window.location.assign("http://"+SuccessPage.extractDomain(window.location.href))
                }
            }else{
                window.location.assign("http://"+SuccessPage.extractDomain(window.location.href))
            }

        },false);
    }
}

【讨论】:

    猜你喜欢
    • 2011-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-03
    • 1970-01-01
    • 2013-05-20
    • 2017-02-06
    • 1970-01-01
    相关资源
    最近更新 更多