Reputation: 31
I'm trying to do that when the link is opened in new tab, there would able back to referrer page through back button.
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);
works fine in MacBook, android chrome/firefox. Only in iPhone/iPad, it doesn't work. It just returns to himself. Even if I add an alert in redirect function it doesn't show up when I click the back button in iPad chrome. :(
Feels like back button click on iPad doesn't fire redirect function.
Upvotes: 3
Views: 2690
Reputation: 21
I've run into the same issue. The work around I've found is not to use replaceState but pushState instead: 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);
Upvotes: 2