Dan*_*ola 12 html5 back-button browser-history html5-history
我有一个页面,其中有几个搜索/过滤按钮,当点击时,通过AJAX刷新下面列表的内容.
在这个过程中,我正在修改历史记录(通过pushstate),以便新的过滤页面可以添加书签,因此后退按钮可以正常工作.我也在听popstate事件,对Back做出反应.
我的代码看起来或多或少像这样:
window.addEventListener("popstate", function(ev) {
if (!window.history_ready) { return; } // Avoid the one time it runs on load
refreshFilter(window.location.href, true);
});
refreshFilter: function(newURL, backButtonPressed){
$.ajax({ url: newURL}).done( blah );
if (!backButtonPressed) {
window.history_ready = true;
history.pushState(null, null, newURL);
}
}
Run Code Online (Sandbox Code Playgroud)
除了一个奇怪的案例外,这种方法效果很好......
至少在最新的Chrome中
为什么会发生这种情况,我该如何避免呢?
pup*_*eno 14
Chrome会缓存您访问的页面,当您返回或转发时,它会使用缓存快速显示页面.如果您用来通过AJAX从服务器检索JSON的URL与Chrome会遇到的URL相同,那么Chrome可能会从缓存中选择该页面,而不是好的HTML,它只是一个JSON转储.
小智 5
有一个缓存选项$.ajax:
$.ajax({ cache: false, url: newURL})
Run Code Online (Sandbox Code Playgroud)
见http://api.jquery.com/jquery.ajax/