PiBoT
PiBoT

Reputation: 41

Browser back button doesn't trigger javascript

I'm trying to create a single-page application.

To allow the user to share links from some specific part of the site, I'm simulating navigation by inserting entries in the browser history.

The problem is, when the user click in the browser's back button, javascript doesn't trigger again. Is possible force it?


I Found the answer in developer.mozilla.org. Just add a listener to 'popstate' event:

window.addEventListener('popstate', function(event) {
 if (event.state) {
   myFunction();
 }
}, false);

"myFunction" will be executed every time the active history entry changes.

Upvotes: 4

Views: 1806

Answers (1)

Datadimension
Datadimension

Reputation: 1045

NO !

Thats the answer !!!! There are many security restrictions in browsers - necessarily, otherwise the internet would be more mayhem than it is. While there are some browser 'back' functionalities available in javascript they are very limited. Browsers should load a page as it was when first opened, and will not pull data from recent to the past. A prime example is the form resubmission warning when going back - a simple check that the step back to previous page cannot be controlled by the page code, instead its the browser stepping in with a check. To expect to do anything more elaborate would be a waste of your time.

Upvotes: -2

Related Questions