Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Event for Back navigation in browser using script

I need to know whether user clicked the back navigation arrow in browser. I Used the below event but not occur this event while i am clicking the back navigation arrow.

$(window).on("navigate", function (event, data) {

});

please suggest your answer If you know.

like image 296
arun d Avatar asked Dec 03 '25 11:12

arun d


1 Answers

You might use the popstate of the history.

The popstate event is only triggered by doing a browser action such as clicking on the back button (or calling history.back() in JavaScript). And the event is only triggered when the user navigates between two history entries for the same document.

You'll have to add a new entry to history with the same title and no change to the url

pushState(state, title, url)

and when you intercept the onpopstate you will do your desired actions, unbind the event and then use the history.back() api.

window.onpopstate = function(event) {
  alert("location: " + document.location + ", state: " + JSON.stringify(event.state));
  window.onpopstate = null;
  history.back();
};

history.pushState({}, document.title, "");

This is not a proven method I've tested it just in Chrome.

like image 104
Sagi Avatar answered Dec 05 '25 01:12

Sagi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!