I use to listen to hashchange handler event. But now with HTML5 you can change the url without using the hash. How can add a listener to url changes?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
hash-changes can happen even without the use of JS. Like the default behaviour of clicking an anchor link can trigger a hash-change.
The HTML5 history object can only be manipulated through JS. For this there is pushState() and replaceState(). Once used, the browser back and forward buttun will trigger the popstate event. There are no other event handlers that I know of for HTML5 History.
If you require a trigger whenever something is pushing states to the history object, then you should rely on the whoever is calling this to allow observers (callbacks) for it.
Otherwise you could try something something not so robust:
http://jsfiddle.net/Cxjey
You could do the same of replace state.
Again, this might not be a good idea as you are changing default browser behaviour.