Skip to content Skip to sidebar Skip to footer

Is There A Callback For History.pushstate?

My Google-fu pulls up nothing. When you do this: var stateObj = { state: 'some state' }; history.pushState(stateObj, 'page 2', 'other.htm'); Is there an associated window callback

Solution 1:

No, there's not a onpushstate or whatever. However, a little monkey patching can fix that:

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

This will only notify you when pushState is used. You'd probably want to do something similar for replaceState.

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


Post a Comment for "Is There A Callback For History.pushstate?"