javascript 每当我执行推送状态时,Statechange 就会触发

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8744487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 04:27:23  来源:igfitidea点击:

Statechange is firing whenever i do a push-state

javascriptjquerybrowser-historyhistory.js

提问by aWebDeveloper

I am using history.js to handle back button. In history.js statechange is firing whenever i do a pushstate. Why?

我正在使用 history.js 来处理后退按钮。在 history.js 中,每当我执行 pushstate 时,statechange 就会触发。为什么?

采纳答案by JaredMcAteer

According to this discussion on github, it's expected behaviour of history.js

根据github 上的讨论,这是 history.js 的预期行为

This pull requestclaims to have modified history.js to be more inline with W3C Specs.

拉取请求声称已修改 history.js 以更符合 W3C 规范。

回答by Anonymous

Wanted to add, yes this is the expected behaviour of History.js. At the same time there are more discussionsthat critize this behaviour as it is not the W3C standard and does create some confusion.

想补充一下,是的,这是 History.js 的预期行为。同时,有更多的讨论批评这种行为,因为它不是 W3C 标准,并且确实造成了一些混乱。

In short, to answer your question: In the History.js pushState()function is a call to statechange at the end.

简而言之,回答您的问题:在 History.jspushState()函数中,最后是对 statechange的调用。

Upsideof this solution is that you can just change (push) your new state and let the onstatechange()-function handle the transition. Downsideis that you are not able to handle exceptions/or have to write them into the onstatechange event-handler.

上攻该解决方案的是,你可以改变(推)的新的状态,让onstatechange() -函数处理的过渡。缺点是您无法处理异常/或必须将它们写入 onstatechange 事件处理程序。

I personally prefer the W3C way of handling this, as you can distinguish between back/forward button and pushState. The History.js maintainers are working on an internal flag solution, that enables you to change this behaviour:

我个人更喜欢 W3C 的处理方式,因为您可以区分后退/前进按钮和 pushState。History.js 维护人员正在研究内部标志解决方案,它使您能够更改此行为:

Notice how the above calls [pushstate-calls] trigger statechange events, if for some reason you do not want this to happen then inside your statechange handler you can use the following:

注意上面的调用 [pushstate-calls] 是如何触发 statechange 事件的,如果由于某种原因你不希望这种情况发生,那么在你的 statechange 处理程序中你可以使用以下内容:

if ( History.getState().internal ) { return; }

*This feature is currently in development and can only be used with the 'dev' version of History.js! Hope this will help some other people in the future :)

*此功能目前正在开发中,只能与 History.js 的“开发”版本一起使用!希望这会在未来帮助其他一些人:)

回答by tommueller

After trying to accomplish this for a day now, I finally found the solution here: https://github.com/browserstate/history.js/issues/47#issuecomment-25750285

在尝试了一天之后,我终于在这里找到了解决方案:https: //github.com/browserstate/history.js/issues/47#issuecomment-25750285

The code is pretty damn simple, the following is quoted from the link:

代码非常简单,下面是从链接中引用的:

When you push your state

当你推动你的状态

History.pushState({
    _index: History.getCurrentIndex(),
    someData: ...
}, someTitle, someUrl);

and then in the event binding

然后在事件绑定中

History.Adapter.bind(window, 'statechange', function () {
    var currentIndex = History.getCurrentIndex();
    var internal = (History.getState().data._index == (currentIndex - 1));
    if (!internal) {
        // your action
    }
});