页面未通过“后退”重新加载当使用pushState() / 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 到以通常方式加载的页面。 URL 将更改,但页面不会重新加载。正如我所期望的那样,再次单击返回会将我带到之前的页面,然后前进就可以正常工作 - 这只是后退按钮不起作用的一种情况。
如有任何建议,我们将不胜感激。
I am refreshing some pages with AJAX and so an updating the history with the following code -
/** Update the page history */
var pushState_object = {
ajax_string: ajax_string,
security: security,
};
window.history.pushState(pushState_object, title, permalink);
I am then calling this onPopState function on page load -
window.onpopstate = function(e){
if(window.history.state !== null){
initiate_load_updated_page(window.history.state.ajax_string, window.history.state.security, 0)
}
}
I am having a slight problem though using the back button when going from a page where the content was generated through AJAX to a page that was loaded in the usual way. The URL will change, but the page will not reload. Clicking back again takes me to the page before, as I would expect, and then going forward works fine - it is just that one situation where the back button does not work.
Any suggestions here would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
初始状态没有可用的 .state 属性,因为您没有使用 .pushState 来添加此类数据(它是按照您描述的正常方式加载的)。
但您确实知道,如果没有
.state
,它一定是原始状态,因此您可以使用else
块,如下所示:http://jsfiddle.net/pimvdb/CCgDn/1/。最后,您可以使用
e.state
。The initial state does not have a
.state
property available, because you didn't use.pushState
to add such data (it was loaded the normal way as you describe).What you do know though, is that if there is no
.state
, it must be the original state, so you can use anelse
block like this: http://jsfiddle.net/pimvdb/CCgDn/1/.Lastly, you can use
e.state
.