【问题标题】:Why popstate doesnt fire my function when back button is clicked? (mobile ios chrome)为什么单击后退按钮时popstate不会触发我的功能? (移动 ios 铬)
【发布时间】:2018-03-10 01:09:20
【问题描述】:

我正在尝试这样做,当在新标签页中打开链接时,可以通过后退按钮返回引荐来源页面。

const referrer = document.referrer;
const redirect = (e) => {
     if(e.state.goBack){
        window.location.href = window.location.href;
     }
 }

const _location = window.location.href
history.replaceState({goBack: true}, null, referrer);
history.pushState({}, null, _location);

window.addEventListener('popstate', redirect);

在 MacBook、android chrome/firefox 中运行良好。 仅在 iPhone/iPad 中不起作用。它只是回到自己身上。即使我在重定向功能中添加警报,当我单击 iPad chrome 中的后退按钮时它也不会显示。 :(

感觉就像在 iPad 上单击后退按钮不会触发重定向功能。

【问题讨论】:

标签: javascript ios iphone reactjs google-chrome


【解决方案1】:

我遇到了同样的问题。我发现的解决方法不是使用 replaceState,而是使用 pushState:https://jsfiddle.net/6dayLhzs/1

const referrer = document.referrer;
const redirect = (e) => {
    if(e.state.goBack){
        window.location.href = window.location.href;
    }
 }

const _location = window.location.href
history.pushState({goBack: true}, null, referrer);
history.pushState({}, null, _location);

window.addEventListener('popstate', redirect);

【讨论】:

    猜你喜欢
    • 2014-08-26
    • 2021-12-25
    • 2013-05-10
    • 1970-01-01
    • 1970-01-01
    • 2011-01-28
    • 1970-01-01
    • 2018-04-15
    • 1970-01-01
    相关资源
    最近更新 更多