是否有History.pushstate的回调? [英] Is there a callback for History.pushstate?
问题描述
我的Google-fu一无所获。
My Google-fu pulls up nothing.
执行此操作时:
var stateObj = { state: "some state" };
history.pushState(stateObj, "page 2", "other.htm");
是否有关联的窗口回调?
Is there an associated window callback?
我知道这就是:
window.onpopstate = function() {
}
当用户点击后退按钮时,它非常适合收听。但是,我想在任何时候听到URL的变化,我不知道该怎么做。
Which works great for listening to when a user hits the back button. However, I want to listen to any time the URL changes at all, and I'm not sure how to do it.
是否有任何时候URL的全局回调更改?
Is there a global callback for anytime the URL changes?
推荐答案
不,没有 onpushstate
或其他什么。但是,一点猴子修补可以解决这个问题:
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
};
这只会在使用pushState时通知您。你可能想要为replaceState做类似的事情。
This will only notify you when pushState is used. You'd probably want to do something similar for replaceState.
如果你需要在URL发生变化时得到通知,上面的一些组合和一个hashchange处理程序将会大部分时间都可以到达那里。
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.
这篇关于是否有History.pushstate的回调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!