0

What I am trying to achieve is to redirect the user to yahoo's page once he has clicked the browser's back button. Here is my attempt, currently nothing happens:

window.addEventListener('popstate', function() {
    var lastVisitedUrl = window.history.go(-1);
    window.history.replaceState(lastVisitedUrl , "Sample Title", "/yahoo.com");
    window.location.href.back()
});

How can I make the redirection once the browser back button is clicked?

  • read this question it might help - https://stackoverflow.com/questions/6359327/detect-back-button-click-in-browser – Satyam Pathak Jul 23 '20 at 08:59
  • You can redirect to yahoo ONLY if the previous page is under your control - there should be easily implemented. You can check [this answer](https://stackoverflow.com/questions/829046/how-do-i-detect-if-a-user-has-got-to-a-page-using-the-back-button). – skobaljic Jul 23 '20 at 09:01

1 Answers1

0

Try this works properly

window.history.pushState({page: 1}, "", "");

window.onpopstate = function(event) {
    if(event){
        window.location.href = 'https://www.yahoo.com';
       
    }
}
Umutambyi Gad
  • 4,082
  • 3
  • 18
  • 39