0

Hi I tried to use the following javascript to warn users.

 window.onbeforeunload = function() {
       return "Are you sure you wish to leave?";
    };

But I need to make sure that the back button is pressed it seems to call this when you change the url.

Thanks

vaughn
  • 41
  • 8
  • this might be helpful https://stackoverflow.com/questions/8980255/how-do-i-retrieve-if-the-popstate-event-comes-from-back-or-forward-actions-with – Corrl Oct 27 '21 at 14:29

2 Answers2

1

you can play with popstate event browser fired when browser history change

window.addEventListener('popstate', function(event) {
  alert('you click on back or forward button');
}

The popstate event is fired when the current history changes :

  • when buttons back/forward are click by user
  • when methods history.back(), history.forward(), history.go() are called.
jeremy-denis
  • 4,594
  • 3
  • 17
  • 26
0

Try looking through these answers here.

So, in short, and for people that aren't necessarily using an in-page back button or an array to store the history:

document.onmouseover = function() {
    //User's mouse is inside the page.
    window.innerDocClick = true;
}

document.onmouseleave = function() {
    //User's mouse has left the page.
    window.innerDocClick = false;
}

window.onhashchange = function() {
    if (window.innerDocClick) {
        //Your own in-page mechanism triggered the hash change
    } else {
        //Browser back button was clicked
    }
}

And there you have it. a simple, three-part way to detect back button usage vs in-page elements with regards to hash navigation.

Your alert would go into the else section of the window.onhashchange handler.