强制Firefox在后退按钮上重新加载页面

Lan*_*ard 19 html javascript firefox caching

当用户按下后退按钮时,如何让Firefox重新运行javascript并重新加载整个页面?通过添加此代码,我可以在除了Firefox之外的所有浏览器中通过另一个SO问题的帮助来执行此操作:

history.navigationMode = 'compatible';
$("body").unload(function(){})
Run Code Online (Sandbox Code Playgroud)

并且还添加了iFrame ...但这在Firefox中不起作用.有什么事可做吗?

jkn*_*air 9

在HEAD标签之间添加此项

<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Expires" CONTENT="-1">
Run Code Online (Sandbox Code Playgroud)

  • 注意:此解决方案不起作用.至少在最近的Chrome和Firefox中没有. (14认同)

Fab*_*olo 8

在我的情况下,在最终用户将他的新数据发布到服务器之后,他被重定向到另一个页面.但是当最终用户按下后退按钮时,表单会预先填充旧数据.因此,需要重新加载页面.

我为Firefox做了一个解决方法,为谷歌Chrome做了另一个解决方法.它们具有显示缓存页面的不同行为.

这样做会阻止Firefox缓存页面,后退按钮会阻止来自服务器的页面.

window.onunload = function(){};
Run Code Online (Sandbox Code Playgroud)

但谷歌Chrome以另一种方式做到这一点,上面的解决方案对我不起作用.所以我为Chrome做了另一个解决方法.我做了一个标志,表格形状很脏.

<head>加载任何内容之前,我检查cookie

if(document.cookie.match(/my-form=dirty/)) {
  document.cookie = "my-form=; expires=-1; path="+document.location.pathname;
  window.location.reload();
}
Run Code Online (Sandbox Code Playgroud)

在jQuery的帮助下,我在用户修改内容时编写cookie

$(document).load(function(){
  $(':input').change(function(){
    document.cookie = "my-form=dirty; expires=86400000; path="+document.location.pathname;
  })
})
Run Code Online (Sandbox Code Playgroud)

很高兴知道:


Yng*_*sen 5

这个解决方案对我有用(我相信比建议的其他解决方案更简单):

    $(window).bind('pageshow', function() {
        // This event is called only by FireFox. Because Firefox doesn't reload the page when the user uses the back button,
        // or when we call history.go.back(), we need to force the reload for the applicatino to work similar to Chrome and IE (11 at least).

        // Doc: https://developer.mozilla.org/en-US/docs/Listening_to_events_in_Firefox_extensions
        location.reload();
    }); 
Run Code Online (Sandbox Code Playgroud)

如果浏览器是 FireFox,我会在每次加载页面时调用此代码。(要了解当前运行的浏览器是否为 Firefox,请参见此处)。