Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 每当我执行推送状态时,Statechange就会触发_Javascript_Jquery_Browser History_History.js - Fatal编程技术网

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

Javascript 每当我执行推送状态时,Statechange就会触发,javascript,jquery,browser-history,history.js,Javascript,Jquery,Browser History,History.js,我正在使用history.js处理后退按钮。在history.js中,每当我执行pushstate时,statechange都会触发。为什么?根据这一点,这是history.js的预期行为 这声称修改了history.js,使其更符合W3C规范。想要添加,是的,这是history.js的预期行为。 同时,也有人批评这种行为,因为它不是W3C标准,确实会造成一些混乱 简而言之,回答您的问题:在History.jspushState()函数中,在末尾调用statechange 此解决方案的优点是,您

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

根据这一点,这是history.js的预期行为


这声称修改了history.js,使其更符合W3C规范。

想要添加,是的,这是history.js的预期行为。 同时,也有人批评这种行为,因为它不是W3C标准,确实会造成一些混乱

简而言之,回答您的问题:在History.js
pushState()
函数中,在末尾调用statechange

此解决方案的优点是,您只需更改(推送)新状态,并让onstatechange()函数处理转换缺点是您无法处理异常/或必须将异常写入onstatechange事件处理程序

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

注意上面的调用[pushstate calls]是如何触发statechange事件的,如果对于某些 您不希望这种情况发生的原因,请在您的州内更改 您可以使用以下方法:


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

在尝试了一天之后,我终于在这里找到了解决方案:

代码非常简单,以下引用自链接:

当你推动你的国家

History.pushState({
    _index: History.getCurrentIndex(),
    someData: ...
}, someTitle, someUrl);
然后在事件绑定中

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

万分感谢。我个人觉得history.js解决方案更好。
History.Adapter.bind(window, 'statechange', function () {
    var currentIndex = History.getCurrentIndex();
    var internal = (History.getState().data._index == (currentIndex - 1));
    if (!internal) {
        // your action
    }
});