是否有History.pushstate的回调?

the*_*ott 11 javascript html5 html5-history

我的Google-fu什么都没有.

当你这样做:

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

是否有关联的窗口回调?

我知道有这个:

window.onpopstate = function() {

}
Run Code Online (Sandbox Code Playgroud)

当用户点击后退按钮时,这非常适合收听.但是,我想在任何时候听到URL的变化,我不知道该怎么做.

URL随时更改时是否存在全局回调?

Pet*_*r C 21

不,没有一个onpushstate或其他什么.但是,一个小猴子修补可以解决这个问题:

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

这只会在使用pushState时通知您.你可能想为replaceState做类似的事情.

如果您需要在URL完全更改时收到通知,则上面的一些组合和hashchange处理程序将为您提供大部分方法.

  • 什么你知道,[这](http://stackoverflow.com/questions/4570093/how-to-get-notified-about-changes-of-the-history-via-history-pushstate)的问题几乎是相同的,并答案也是如此.:P (2认同)