javascript 按下后退按钮时,如何让 Firefox 重新加载页面?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4630484/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I make Firefox reload page when back button is pressed?
提问by ubiquibacon
I have tried every combination and permutation of meta tags that are supposed to stop a page from being cached, but Firefox STILL caches the page! I just need the URL to reload when a user presses the back button.Works fine in IE8.
我已经尝试了应该阻止页面被缓存的元标记的所有组合和排列,但 Firefox 仍然缓存页面! 当用户按下后退按钮时,我只需要重新加载 URL。在 IE8 中工作正常。
I have tried all of these...
我已经尝试了所有这些......
<meta http-equiv="Cache-Control" content="no-store" />
<meta http-equiv="Pragma" content="no-cache"/>
<meta http-equiv="Expires" content="-1"/>
<meta http-equiv="Expires" content="Sat, 1 Jan 2000 00:00:00 GMT" />
...and I have also tried the following JavaScript...
...而且我还尝试了以下 JavaScript ...
<input type="hidden" id="refreshed" value="no"/>
<script type="text/javascript">
onload=function(){
var e=document.getElementById("refreshed");
if(e.value=="no"){
e.value="yes";
}
else{
e.value="no";
location.reload();
}
}
</script>
... all to no avail. What am I missing here? Pages are generated with PHP if that matters.
……一切都无济于事。我在这里错过了什么?如果重要的话,页面是用 PHP 生成的。
UPDATE 1:
更新 1:
I have tried every suggestion thus far but I still cannot get this to work. When I use Chris's PHP code I use it like this...
到目前为止,我已经尝试了所有建议,但我仍然无法让它发挥作用。当我使用 Chris 的 PHP 代码时,我是这样使用的......
<?php
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html dir="ltr" xmlns="http://www.w3.org/1999/xhtml">
<!--the rest of my page-->
.. and as you can see it is at the EXTREME top of my webpage, before the DOCTYPEheader.
.. 正如您所看到的,它位于我网页的 EXTREME 顶部,在DOCTYPE标题之前。
I have also experimented with session_start()but even after reading the manual I am not sure I am using it right. I was putting it right at the very top of my page as well.
我也尝试过,session_start()但即使阅读了手册,我也不确定我是否正确使用它。我也把它放在我页面的最顶部。
I am open to ANY SUGGESTIONS that make this work without breaking other page functionality. I know I have seen pages that reload EVERY TIME the back button is used, HOW ARE THEY DOING IT?!
我愿意接受任何可以在不破坏其他页面功能的情况下完成这项工作的建议。我知道我见过每次使用后退按钮时都会重新加载的页面,他们是怎么做的?!
SOLVED!
解决了!
Turns out I had multiple issues working against me, but through due diligence I was able to eliminate those issues and emerge victorious.
事实证明,我有多个问题对我不利,但通过尽职调查,我能够消除这些问题并取得胜利。
After Chris updated his code to...
在 Chris 将他的代码更新为...
<?php
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
echo time();
?><a href="http://google.com">aaaaaaaaaaaaa</a>
I found that his code did indeed work when I used it EXACTLY how he had it with NOTHING else, but when I put it into my pages it didn't work. All of my pages are either .phpor .htmland all are attached to a DWT (Dynamic Web Template), so I was updating all of them at once with Chris's code. What I didn't realize was that the DWT starts RIGHT AFTER the DOCTYPEheader, so the code was never inserted into my pages. I could find no way to make the DWT include the DOCTYPEheader so I went into all my pages and manually inserted the code above the DOCTYPEheader.
我发现他的代码确实在我使用它时确实有效,就像他没有其他任何东西一样,但是当我将它放入我的页面时它不起作用。我的所有页面都是.php或.html都附加到 DWT(动态 Web 模板),因此我使用 Chris 的代码一次更新所有页面。我没有意识到 DWT 是在DOCTYPE标题之后立即启动的,因此代码从未插入到我的页面中。我找不到让 DWT 包含DOCTYPE标题的方法,所以我进入了所有页面并手动将代码插入到DOCTYPE标题上方。
Next I found that even though my server is set to parse .htmand .htmlas .phpthe .htmlpages were generating an error at the very where I had inserted Chris's code saying something to the effect of "cannot modify headers, headers have already been sent". I didn't really care what my extensions were so I just changed all my .htmlextensions to .phpextensions.
接下来,我发现,即使我的服务器设置为解析.htm并.html作为.php该.html页面均在十分产生一个错误,我插了克里斯的代码,说的东西的影响“不能修改标题,标题已发送”。我并不真正关心我的扩展是什么,所以我只是将所有.html扩展更改为.php扩展。
A final minor annoyance was that even though the page was now not being cached (just like I wanted) Firefox was placing the user at their last location on the previous page when they used the back button (i.e. if a user was at the bottom of page a when they navigated to page b, then the user used the back button on page b they would be returned to the bottom of page a, not the top of page a as desired). Trimming down my original JavaScript fixed this...
最后一个小烦恼是,即使页面现在没有被缓存(就像我想要的那样),当他们使用后退按钮时,Firefox 将用户放在上一页的最后一个位置(即,如果用户在底部当他们导航到页面 b 时,用户使用页面 b 上的后退按钮,他们将返回到页面 a 的底部,而不是根据需要返回到页面 a 的顶部)。修剪我原来的 JavaScript 解决了这个问题......
<script type="text/javascript">
onload=function(){
document.getElementById('content').scrollTop=0;
}
</script>
Even though this seems very involved for such a simple problem I am glad it is fixed. Thanks for your help everyone (especially Chris).
尽管这对于这样一个简单的问题来说似乎很复杂,但我很高兴它得到了解决。感谢大家的帮助(尤其是克里斯)。
回答by goat
This works for me
这对我有用
make a new php file. I can use the back and forward buttons and the number on the page always updates
新建一个php文件。我可以使用后退和前进按钮,页面上的数字总是更新
<?php
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
echo time();
?><a href="http://google.com">aaaaaaaaaaaaa</a>
回答by Abhishek Shringi
You may add the following PHP Code in your PHP Page.
您可以在 PHP 页面中添加以下 PHP 代码。
CodeIgniter Framework version:
$this->output->set_header('Last-Modified:'.gmdate('D, d M Y H:i:s').'GMT');
$this->output->set_header('Cache-Control: no-store, no-cache, must-revalidate');
$this->output->set_header('Cache-Control: post-check=0, pre-check=0',false);
$this->output->set_header('Pragma: no-cache');
PHP version:
PHP版本:
header('Last-Modified:'.gmdate('D, d M Y H:i:s').'GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0',false);
header('Pragma: no-cache');
And also you may add following Java Script Code in Login/Master Page at <head>Section:
您也可以在登录/主页中的<head>部分添加以下 Java 脚本代码:
<script language="javascript" type="text/javascript">
window.history.forward();
</script>
Compatibility verified in
兼容性验证
- Internet Explorer
- Google Chrome
- Mozilla Firefox
- Opera
- IE浏览器
- 谷歌浏览器
- 火狐浏览器
- 歌剧
回答by profitphp
Check out live http headers, you'll see when you press the back button, no request is made. Which is done on purpose, so you don't loose what you may have typed into forms.
查看实时 http 标头,您会看到当您按下后退按钮时,没有发出任何请求。这是故意完成的,因此您不会丢失您可能已输入表单的内容。
Edit
编辑
Some post on experts exchange said this worked, but its from 2006.
专家交流上的一些帖子说这有效,但它是从 2006 年开始的。
<META HTTP-EQUIV="Expires" CONTENT="0">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
<META HTTP-EQUIV="Cache-Control" CONTENT="no-store">
Then a javascript refresh
然后一个javascript刷新
回答by nedk
<meta http-equiv="Cache-Control" content="no-cache" />
I think the correct value is "no-cache"
我认为正确的值是“无缓存”
回答by kijin
session_cache_limiter('nocache');
That should automatically generate all the required headers. Note that some browsers, such as Opera, still ignore these when using the back button.
这应该会自动生成所有必需的标题。请注意,某些浏览器(例如 Opera)在使用后退按钮时仍会忽略这些。
EDIT
编辑
The relevant headers are as follows, in case you want to generate them some other way:
相关标题如下,以防您想以其他方式生成它们:
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
回答by Ian
Why? I mean, why do you want to do this? When I press the back button, I want the standard function that the browser provides - to go back to the previous page. I don't want a suprise.
为什么?我的意思是,你为什么要这样做?当我按下后退按钮时,我想要浏览器提供的标准功能 - 返回上一页。我不想要一个惊喜。
If it is to stop an accidental resubmission of a POST, that can be accomplished easily by ending the POST handling with a redirect to display the desired reply using a GET. A later back into this page, will redisplay the redirected GET (or reload it from the cache which must have the same result). This is what the user expects and is harmless.
如果要停止意外重新提交 POST,可以通过使用 GET 重定向结束 POST 处理以显示所需的回复来轻松完成。稍后返回此页面,将重新显示重定向的 GET(或从必须具有相同结果的缓存中重新加载它)。这是用户所期望的并且是无害的。
This has the added advantage that if your save has failed when updating the database, the display will display what is on the database, not what has been left in the variables.
这有一个额外的好处,如果您在更新数据库时保存失败,则显示将显示数据库中的内容,而不是变量中剩余的内容。
回答by PeeHaa
You posted a comment:
你发表了评论:
It is because once the form has been submitted, IF the user presses the back button, the information they are viewing will be inaccurate.
这是因为一旦表单提交后,如果用户按下后退按钮,他们正在查看的信息将不准确。
If you want to prevent this you're doing it wrong! :)
如果你想阻止这种情况,那你就错了!:)
What you need to do is a redirect when the form has been submitted.
您需要做的是在提交表单后进行重定向。
<?php
// code that handles the form submit
header("Location: http://www.example.com/form-success.html"); // needs the absolute url (You can use $_SERVER['HTTP_HOST'], $_SERVER['PHP_SELF'] and dirname() for this)
exit; // prevents further execution of the php code when while doing to redirect
?>

