Is there a callback for History.pushstate?

My google fu pulled nothing.

When you do this:

var stateObj = { state: "some state" }; history.pushState(stateObj, "page 2", "other.htm"); 

Is there a related window callback?

I know what is it:

 window.onpopstate = function() { } 

Which is great for listening when the user clicks the back button. However, I want to listen to any changes in the URL at all times, and I'm not sure how to do this.

Is there a global callback to change the url?

+6
javascript html5 html5-history
May 02 '12 at 18:52
source share
1 answer

No, no onpushstate or anything else. However, a small monkey fix can fix this:

 var pushState = history.pushState; history.pushState = function () { pushState.apply(history, arguments); fireEvents('pushState', arguments); // Some event-handling function }; 

This will notify you only when using pushState. You probably want to do something similar for replaceState.

If you need to be notified when the URL changes at all, some combination of the above and the hashchange handler will get you most of the way.

+12
May 2 '12 at 18:58
source share



All Articles