4

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.

2
  • 1
    You need to add more details? Are you using a routing library or trying to access the html5 history api directly? Commented Jul 22, 2014 at 18:39
  • I'm changing the current url using: window.history.pushState("Object", "Title", myUrl +"search=something");
    – PiBoT
    Commented Jul 22, 2014 at 20:55

1 Answer 1

-2

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.

3
  • 2
    That's plainly incorrect, given things like the HTML5 history API.
    – cloudfeet
    Commented Jul 22, 2014 at 18:44
  • woah calm down !!! I was only giving experience from trying something similar a few months ago. Browsers are browsers and javascript is not HTML5. Best bet is to AJAX to a database as you go to collect actions. Unsure what you mean by inappropriate - I am just giving plain experience where there are no specifics in the Q. Commented Jul 22, 2014 at 18:49
  • @TimHigham What they're saying is that using the HTML5 History API in JavaScript, you should be able to achieve what the OP is saying. I mean, this is the basis of AngularJS's routing (using hash tags, or the history API)
    – Ian
    Commented Jul 22, 2014 at 19:04

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.