我在新标签页中打开链接时尝试这样做,可以通过后退按钮返回引荐页面。
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上的后退按钮点击不会激发重定向功能。
答案 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);