为什么popstate在单击后退按钮时不会激活我的功能? (手机ios chrome)

时间:2017-09-28 10:15:36

标签: javascript ios iphone reactjs google-chrome

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

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上的后退按钮点击不会激发重定向功能。

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题。我发现的工作不是使用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);