php 按下后退按钮时强制重新加载/刷新
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9071838/
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
Force reload/refresh when pressing the back button
提问by FlyingCat
I will try my best to explain this.
我会尽力解释这一点。
I have an application that show the 50+ projects in my view
page. The user can click the individual project and go to the update
page to update the project information. Everything works fine except that after user finish updating the individual project information and hit 'back' button on the browser to the previous view page
. The old project information (before update) is still there. The user has to hit refresh to see the updated information. It not that bad, but I wish to provide better user experience. Any idea to fix this? Thanks a lot.
我有一个应用程序,可以在我的view
页面中显示 50 多个项目。用户可以点击单个项目,进入update
页面更新项目信息。一切正常,除了在用户完成更新单个项目信息并在浏览器上点击“返回”按钮到上一个view page
. 旧的项目信息(更新前)还在。用户必须点击刷新才能看到更新的信息。还不错,但我希望提供更好的用户体验。有什么想法可以解决这个问题吗?非常感谢。
采纳答案by Loupax
I think you must work with JS to make this work, since there is no way for PHP to know what browser controlls the user has access to...
我认为您必须使用 JS 才能完成这项工作,因为 PHP 无法知道用户可以访问哪些浏览器控件......
Maybe this will help: http://www.hunlock.com/blogs/Mastering_The_Back_Button_With_Javascript
也许这会有所帮助:http: //www.hunlock.com/blogs/Mastering_The_Back_Button_With_Javascript
回答by phper
I use these four lines of PHP code:
我使用这四行PHP代码:
// any valid date in the past
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
// always modified right now
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
// HTTP/1.1
header("Cache-Control: private, no-store, max-age=0, no-cache, must-revalidate, post-check=0, pre-check=0");
// HTTP/1.0
header("Pragma: no-cache");
The key is using the "no-store" clause of the Cache-Control header.
关键是使用 Cache-Control 标头的“no-store”子句。
回答by abriggs
The "fb-cache" can be really annoying. Here is a simple javascript way around it:
“fb-cache”真的很烦人。这是一个简单的javascript方法:
window.onpageshow = function(evt) {
// If persisted then it is in the page cache, force a reload of the page.
if (evt.persisted) {
document.body.style.display = "none";
location.reload();
}
};
Tested in Safari 6 and IE10.
在 Safari 6 和 IE10 中测试。
回答by Rohit
if (window.name == "reloader") {
window.name = "";
location.reload();
}
window.onbeforeunload = function() {
window.name = "reloader";
}
Add this two functions in every pages
在每个页面中添加这两个功能
回答by josef.van.niekerk
Fortunately, we don't have to support browsers below IE10, so if you're willing or privileged enough to support only browsers that are HTML5 capable, the following should work:
幸运的是,我们不必支持 IE10 以下的浏览器,所以如果您愿意或有足够的特权只支持支持 HTML5 的浏览器,以下应该可以工作:
/*
* The following is intentional, to force Firefox to run
* this JS snippet after a history invoked back/forward navigation.
*/
window.onunload = function(){};
function formatTime(t) {
return t.getHours() + ':' + t.getMinutes() + ':' + t.getSeconds();
}
if (window.history.state != null && window.history.state.hasOwnProperty('historic')) {
if (window.history.state.historic == true) {
document.body.style.display = 'none';
console.log('I was here before at ' + formatTime(window.history.state.last_visit));
window.history.replaceState({historic: false}, '');
window.location.reload();
} else {
console.log('I was forced to reload, but WELCOME BACK!');
window.history.replaceState({
historic : true,
last_visit: new Date()
}, '');
}
} else {
console.log('This is my first visit to ' + window.location.pathname);
window.history.replaceState({
historic : true,
last_visit: new Date()
}, '');
}
Well, here's the code without comments and flab:
好吧,这是没有注释和松弛的代码:
window.onunload = function(){};
if (window.history.state != null && window.history.state.hasOwnProperty('historic')) {
if (window.history.state.historic == true) {
document.body.style.display = 'none';
window.history.replaceState({historic: false}, '');
window.location.reload();
} else {
window.history.replaceState({historic : true}, '');
}
} else {
window.history.replaceState({historic : true}, '');
}
Stick that just before your closing body tag, and you'll have a fairly clean solution.
把它贴在结束身体标签之前,你就会有一个相当干净的解决方案。
This will work with any combination of back/forward being clicked, and the moment the user lands on a new page, no forced reloading will occur.
这将适用于单击后退/前进的任何组合,并且当用户登陆新页面时,不会发生强制重新加载。
Read more about the History object on MDN:
在 MDN 上阅读更多关于 History 对象的信息:
回答by jahackbeth
None of the above solutions worked for me. This simple solution posted hereworked ...
以上解决方案都不适合我。这里发布的这个简单的解决方案有效......
I've tried to force a page to be downloaded again by browser when user clicks back button, but nothing worked. I appears that modern browsers have separate cache for pages, which stores complete state of a page (including JavaScript generated DOM elements), so when users presses back button, previous page is shown instantly in state the user has left it. If you want to force browser to reload page on back button, add onunload="" to your (X)HTML body element:
当用户单击后退按钮时,我试图强制浏览器再次下载页面,但没有任何效果。我似乎现代浏览器有单独的页面缓存,它存储页面的完整状态(包括 JavaScript 生成的 DOM 元素),因此当用户按下后退按钮时,上一页会立即显示为用户已离开的状态。如果您想强制浏览器在后退按钮上重新加载页面,请将 onunload="" 添加到您的 (X)HTML body 元素:
<body onunload="">
<body onunload="">
This disables special cache and forces page reload when user presses back button. Think twice before you use it. Fact of needing such solution is a hint your site navigation concept is flawed.
这将禁用特殊缓存并在用户按下后退按钮时强制页面重新加载。使用前请三思。需要此类解决方案的事实表明您的网站导航概念存在缺陷。
回答by mhd
after days of searching on the net to find a solution, i finally figure it out, add after:
经过几天在网上搜索找到解决方案,我终于弄清楚了,在后面添加:
<script>
window.onbeforeunload = function(){window.location.reload();}
</script>
this will work like a charm you will thank me
这会像魅力一样工作,你会感谢我
this code will reload the page whenever you visit it.
每当您访问它时,此代码都会重新加载页面。
回答by Paul Appleby
This is the solution I have used:
这是我使用的解决方案:
<?php
session_start();
if (!$_SESSION['reloaded']) {$_SESSION['reloaded'] = time();}
else if ($_SESSION['reloaded'] && $_SESSION['reloaded'] == time()) { ?>
<script>
location.reload();
</script>
<?php } ?>
回答by gekong
here's a solution that ive used in some of my pages. add this to pages that changes are made at.
这是我在我的一些页面中使用的解决方案。将此添加到进行更改的页面。
window.onbeforeunload = function() {
window.name = "reloader";
}
this triggers when you leave those pages. you can also trigger it if there were changes made. so that it wont unnecessarily reload the page that needs reloading. then on pages that you want to get reloaded on after a the browser "back" use.
这在您离开这些页面时触发。如果进行了更改,您也可以触发它。这样它就不会不必要地重新加载需要重新加载的页面。然后在浏览器“返回”使用后要重新加载的页面上。
if (window.name == "reloader")
{
window.name = "no";
reloadPage();
}
this will trigger a reload on the page you need reloading to... hope this helps :) oh btw this is in js.
这将在您需要重新加载的页面上触发重新加载...希望这会有所帮助:) 哦顺便说一句,这是在 js 中。
回答by James
Well, you could either provide a built-in back button on the update page that will send them to it new (like a link <a href="<?= $_SERVER['HTTP_REFERRER']; ?>">BACK</a>
or put some script on the page that will force it to pull data every time the page is hit, whether it's a new impression, or if the back button was used.
好吧,您可以在更新页面上提供一个内置的后退按钮,将它们发送到新页面(例如链接<a href="<?= $_SERVER['HTTP_REFERRER']; ?>">BACK</a>
或在页面上放置一些脚本,这将强制它在每次点击页面时提取数据,无论是新印象,或者是否使用了后退按钮。