Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/79.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 页面未通过'重新加载;背面';使用pushState()/onpopstate时_Javascript_Html_Pushstate - Fatal编程技术网

Javascript 页面未通过'重新加载;背面';使用pushState()/onpopstate时

Javascript 页面未通过'重新加载;背面';使用pushState()/onpopstate时,javascript,html,pushstate,Javascript,Html,Pushstate,我正在使用AJAX刷新一些页面,并使用以下代码更新历史记录- /** Update the page history */ var pushState_object = { ajax_string: ajax_string, security: security, }; window.history.pushState(pushState_object, title, permalink); 然后在页面加载时调用这个onPopState函数- window.onpopstate

我正在使用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生成内容的页面转到一个以通常方式加载的页面时,使用back按钮有一个小问题。URL将更改,但页面不会重新加载。如我所料,再次单击“上一步”会将我带到前面的页面,然后继续前进,效果很好——这只是“上一步”按钮不起作用的一种情况


此处的任何建议都将不胜感激。

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

但是,您知道的是,如果没有
.state
,则它必须是原始状态,因此您可以使用如下
else
块:

最后,您可以使用
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
    }
}

谢谢你的重播,你解释的方式帮助我理解了这个问题。我没有使用
else
建议,而是在最初加载页面时使用了
replaceState()
,以确保存在正确的
.state
。我这样做是因为我真的不知道如何才能通过JS重新加载页面内容。也许这不是最优雅的方式,但它确实有效。谢谢你的帮助。@DavidGard你能告诉我你是如何解决这个问题的吗。。。我有一个类似的问题,我不确定如何使用
replaceState()
来解决it@NickGinato-哇,这是一年半以前的事了!我会尝试挖掘一些东西,但没有承诺。在上面的解决方案中,如果有其他人正在努力,只需将initiate_load_updated_page行更改为location.reload();谢谢