Javascript “location.reload()”丢失 POST/SESSION 数据?(F5 / Ctrl+R 保留数据?)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10861176/
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
"location.reload()" loses POST/SESSION data? (F5 / Ctrl+R keeps data?)
提问by Zorkzyd
I want to create a button to reload a page without losing $_POST
data and $_SESSION
.
On the web, I found this piece of code:
我想创建一个按钮来重新加载页面而不会丢失$_POST
数据和$_SESSION
.
在网上,我找到了这段代码:
onclick="document.location.reload();"
And here is the code of my button:
这是我的按钮的代码:
<a class="button" href="" style="font-size: 0.7em; padding: 5px 10px;" onclick="document.location.reload();">Recharger la page</a>
But when I click on the button, I lose $_POST
data and $_SESSION
.
但是当我点击按钮时,我丢失了$_POST
数据和$_SESSION
.
If I try with the keyboard command Ctrl+R(Chrome) or F5(Firefox, IE9), the browser is showing an alert to notify me that I'm again trying to submit form. If I accept, it works.
如果我尝试使用键盘命令Ctrl+R(Chrome) 或F5(Firefox、IE9),浏览器会显示警报,通知我我再次尝试提交表单。如果我接受,它就起作用了。
How can I reproduce this kind of browser-refresh with a JavaScript command? Or is the code of my button wrong?
如何使用 JavaScript 命令重现这种浏览器刷新?还是我按钮的代码错了?
Thank you very much for your help.
非常感谢您的帮助。
回答by user2428118
Try using
尝试使用
location.reload(true);
This will perform a "hard" refresh, not just rebuilding the DOM but also re-retrieving any resource from the server.
这将执行“硬”刷新,不仅重建 DOM,还会从服务器重新检索任何资源。
You can read more at the Mozilla Developer wiki.
您可以在 Mozilla Developer wiki 上阅读更多内容。
Apparently, location.reload()
is the equivalent of F5
in scripting, whilst Ctrl+F5
/ Ctrl+R
can be simulated using location.reload(true)
.
显然,location.reload()
相当于F5
在脚本中,而Ctrl+F5
/Ctrl+R
可以使用location.reload(true)
.
Also, as ThiefMaster mentioned, you're missing ;return false
at the end of your onclick
statement, or you should set the href
to javascript:void 0
*to prevent the browser from following the link.
此外,正如 ThiefMaster 所提到的,您在语句;return false
的末尾丢失了onclick
,或者您应该将 设置href
为javascript:void 0
*以防止浏览器跟随链接。
*Or any other piece of JavaScript that returns undefined
* 或任何其他返回的 JavaScript undefined
回答by ThiefMaster
This should happen in any case as long as you are on the same location you POSTed to. However, it's common to redirect after a POST request to avoid exactly what you are trying to do.
只要您在发布到的同一位置,这在任何情况下都应该发生。但是,通常在 POST 请求后重定向以避免您尝试执行的操作。
The reason why your code doesn't work is the fact that href=""
will cause a GET request to the current URL. Use href="#"
to prevent it from loading a "new" page or add return false;
at the end of your onclick="..."
code.
您的代码不起作用的原因是href=""
会导致对当前 URL 的 GET 请求。使用href="#"
以防止它加载一个“新”页面或添加return false;
在你结束onclick="..."
代码。
回答by Tschareck
The Ctrl + R
refreshes the page andclears your cache. And I guess you're using Internet Explorer? Some other browsers behave like this when you hit Ctrl + F5
, but not with Ctrl + R
该Ctrl + R
刷新页面并清除缓存。我猜你正在使用 Internet Explorer?当你点击 时Ctrl + F5
,其他一些浏览器的行为是这样的,但不是Ctrl + R
Sources:
https://superuser.com/questions/205279/ctrlf5-vs-ctrlr-on-browsers
Browser issue in Ctrl-R
来源:
https: //superuser.com/questions/205279/ctrlf5-vs-ctrlr-on-browsers
Ctrl-R 中的浏览器问题