Search code examples
javascriptbrowser-history

How to determine browser forward or backward in ajax app


I have an ajax application that uses hashbanging for navigation and I keep track of the navigation stack in my application. Is there any good way of determine if the user presses the back button in the browser or just pressing a link going to the same url.

Example: Lets assume the user is at http://mysite.com/#home, and then presses a link going to http://mysite.com/#settings. This new settings page contains a link to http//mysite.com/#home. How can i determine if the user presses the link to go back to home or if the users presses the back button on the browser.

Since I internally keep track of the history stack I would like to know if I should push or pop the url of the stack.

One solution would be to poison the url with a counter or timestamp, but it would be nice to avoid.


Solution

  • Forget about hashbang and use html5 pushState. When the page history changes, he popState event is fired.