2010-08-21 130 views
19

如何让Firefox重新运行JavaScript并在用户按下后退按钮时重新加载整个页面?我能做到这一点,从另一个SO问题的帮助,除了火狐所有的浏览器中加入以下代码:强制Firefox重新加载后退按钮上的页面

history.navigationMode = 'compatible'; 
$("body").unload(function(){}) 

,并且还加入一个iFrame ......但是,这并不工作在Firefox。有什么事吗?

回答

7

你的脑袋标签之间添加此

<META HTTP-EQUIV="Pragma" CONTENT="no-cache"> 
<META HTTP-EQUIV="Expires" CONTENT="-1"> 
+12

注意:此解决方案不起作用。至少不是在最近的Chrome和Firefox中。 – 2013-02-20 21:02:27

+0

至于为什么它不起作用:http://stackoverflow.com/questions/10314174/difference-between-pragma-and-cache-control-headers – thomas88wp 2016-01-16 14:06:25

1
+0

第二个链接(演示)被阻止。 – 2013-03-28 17:39:32

+0

因为这不是我的演示,所以我什么也做不了...... – gearsdigital 2013-04-07 14:26:37

+0

由于所有的链接现在都被破坏,答案几乎没有用。这就是为什么你总是应该引用来自你的来源的相关部分,而不仅仅是发布链接。 – Basti 2014-08-08 08:53:12

3

作为参考,即navigationMode属性只适用于歌剧院,它只是IE和Webkit浏览器已经具有提问者想要的行为。

IE和Webkit做什么Opera会称之为“兼容”导航。 Firefox会做什么Opera会称之为“快速”导航。但只有在Opera中,这种行为实际上是可控的。

0

这一切都为我工作。我通过cookie检查 - 我在页面的头部导入jsp(源代码如下),然后我调用refreshOnBack。

<%@ page import="java.util.UUID" %> 
<script> 
    var REFRESH_PAGE_COOKIE_NAME="refreshPage_"; 
    var PAGE_REFRESH_GUID = "<%out.print(UUID.randomUUID().toString());%>"; 
    /* To force refresh of page when user or javascript clicks "back", 
    * put call to this method in body onload function - NOT THROUGH JQUERY 
    * as jquery onload is not called on history back. 
    * In must be done via <BODY onload="..."> 
    */ 
    function refreshOnBack(){ 
     //Alghoritm uses cookies to check if page was already loaded. 
     //Cookies expiration time is not set - will be gone after browser restart. 
     //Alghoritm: 
     //is cookie set? 
     // -YES - reload; 
     // -NO - set it up; 
     //Cookie contains unique guid in name so that when page is regenerated - it will not reload. 
     var cookieName = REFRESH_PAGE_COOKIE_NAME + PAGE_REFRESH_GUID; 
     if(getCookie(cookieName)=="Y"){ 
      window.location.reload(); 
     } else { 
      setCookie(cookieName,"Y"); 
     } 
    } 
</script> 
+0

你怎么称呼refreshOnBack? – 2013-03-28 17:45:29

6

就我而言,在最终用户将他的新数据发布到服务器之后,他被重定向到另一个页面。但是当最终用户按下后退按钮时,表单会预先填入旧数据。所以,需要重新加载页面。

我为Firefox做了一个解决方法,而Google Chrome则做了另一个解决方法。他们有不同的行为来显示缓存页面。

这样做会阻止Firefox缓存页面,并且后退按钮将从服务器中获取页面。

window.onunload = function(){}; 

但谷歌浏览器以另一种方式做,上述解决方案不适合我。所以我为Chrome做了另一个解决方法。我做了一个标志,标志着表格很脏。

截至<head>顶部,负载任何事情之前,我检查Cookie

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

使用jQuery的帮助下,我写的cookie时用户可以修改一些

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

好消息:

+0

window.onunload = function(){}与Angular结合使用。如前面的回答添加meta标签没有。 – eeejay 2015-09-14 23:13:01

0

我想你可能想答案就在这里:https://stackoverflow.com/a/5493543/1475148

TLDR:HTTPS + Cache-Control: must-revalidate作为服务器发送头+ Expires头在过去的设定应该使其工作。下面是一个示例PHP实现:

<?php 

$expires = gmdate('D, d M Y H:i:s', time() - 1); 

header('Cache-Control: must-revalidate'); 
header("Expires: {$expires} GMT"); 

?> 
<!DOCTYPE html> 
<html> 
    <head> 
     <title>Auto-reloading page</title> 
    </head> 
    <body> 
     <p>The current day is <?php echo date('d, F Y'); ?> and the time is <?php echo date('H:i:s'); ?>.</p> 
     <p><a href="http://google.com">Click away</a></p> 
    </body> 
</html> 
+0

FF中不工作18.0.2 – 2013-08-08 11:06:50

4

该解决方案为我(我相信这是一个有点比其他解决方案更简单的建议):

$(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(); 
    }); 

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

+0

我工作;-)谢谢 – jobima 2015-10-02 08:20:30

相关问题