aWe*_*per 15 javascript jquery browser-history history.js
我正在使用history.js来处理后退按钮.在history.js中,只要我执行pushstate,statechange就会触发.为什么?
Ano*_*ous 12
想添加,是的,这是History.js的预期行为.与此同时,有更多的讨论批评这种行为,因为它不是W3C标准,确实造成了一些混乱.
简而言之,回答你的问题:在History.js pushState()函数中最后调用statechange.
上攻该解决方案的是,你可以改变(推)的新的状态,让onstatechange() -函数处理的过渡.缺点是您无法处理异常/或必须将它们写入onstatechange事件处理程序.
我个人更喜欢W3C的处理方式,因为你可以区分后退/前进按钮和pushState.History.js维护者正在开发内部标记解决方案,使您可以更改此行为:
注意上面调用[pushstate-calls]如何触发statechange事件,如果由于某种原因你不希望发生这种情况,那么在statechange处理程序中你可以使用以下代码:
if ( History.getState().internal ) { return; }
Run Code Online (Sandbox Code Playgroud)
*此功能目前正在开发中,只能与History.js的'dev'版本一起使用!希望这将有助于未来的其他人:)
在尝试完成这一天之后,我终于找到了解决方案:https://github.com/browserstate/history.js/issues/47#issuecomment-25750285
代码很简单,下面引用链接:
当你推动你的状态
History.pushState({
_index: History.getCurrentIndex(),
someData: ...
}, someTitle, someUrl);
Run Code Online (Sandbox Code Playgroud)
然后在事件绑定中
History.Adapter.bind(window, 'statechange', function () {
var currentIndex = History.getCurrentIndex();
var internal = (History.getState().data._index == (currentIndex - 1));
if (!internal) {
// your action
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10139 次 |
| 最近记录: |