如何更改状态而不触发history.js中的状态更改?

jgi*_*oni 8 ajax jquery history.js

我正在使用history.js,我有一个事件处理程序

$(window).bind('statechange', function(){
    // Loads the content representing the state via ajax
});
Run Code Online (Sandbox Code Playgroud)

History.pushState([...])用户单击链接或按钮时会触发大多数内容更改.

但在某些情况下,内容更改由javascript直接管理(即通过更改,添加或删除DOM元素),并且不需要通过ajax加载表示新状态的内容.

仍然,我需要推送状态并更改URL以反映新状态,如果用户点击重新加载或稍后想要使用后退按钮等.

所以我的问题是:如何推送状态但在某些情况下避免加载内容?是否有一种可以传递给statechange事件处理程序的标志,还是我可以完全绕过它?

ser*_*g66 7

我也想知道这个问题.我的决定是使用全局变量.例如,您可以将window.stateChangeIsLocal初始化为false:

window.stateChangeIsLocal = false;
Run Code Online (Sandbox Code Playgroud)

您的statechange处理程序可能看起来像这样:

History.Adapter.bind(window,'statechange',function(){
    if (!window.stateChangeIsLocal) {
        someAjaxLoadFunction(History.getState().url);
    }
    else {
        window.stateChangeIsLocal = false;
    }
});
Run Code Online (Sandbox Code Playgroud)

当您更改状态并且不想加载新状态的内容时,只需设置window.stateChangeIsLocaltrue;

也许有一些更优雅的解决方案,但我找不到它们并使用它.