20

我正在使用 AJAX 刷新一些页面,因此使用以下代码更新历史记录 -

/** Update the page history */
var pushState_object = {
    ajax_string: ajax_string,
    security: security,
};
window.history.pushState(pushState_object, title, permalink);

然后我在页面加载时调用这个 onPopState 函数 -

window.onpopstate = function(e){
    if(window.history.state !== null){
        initiate_load_updated_page(window.history.state.ajax_string, window.history.state.security, 0)
    }
}

从通过 AJAX 生成内容的页面转到以通常方式加载的页面时,虽然使用后退按钮,但我遇到了一个小问题。URL 将更改,但页面不会重新加载。正如我所期望的那样,再次单击返回会将我带到之前的页面,然后继续工作正常 - 这只是返回按钮不起作用的一种情况。

这里的任何建议将不胜感激。

4

1 回答 1

19

初始状态没有.state可用的属性,因为您没有使用.pushState添加此类数据(它是按照您描述的正常方式加载的)。

不过,您知道的是,如果没有.state,它必须是原始状态,因此您可以使用这样的else块:http: //jsfiddle.net/pimvdb/CCgDn/1/

最后,您可以使用e.state.

window.onpopstate = function(e){
    if(e.state !== null) { // state data available
        // load the page using state data
        initiate_load_updated_page(e.state.ajax_string, window.history.state.security, 0);

    } else { // no state data available
        // load initial page which was there at first page load
    }
}
于 2011-10-25T12:03:25.677 回答