" location.reload()"丢失POST / SESSION数据? (F5 / Ctrl + R保存数据?) [英] "location.reload()" loses POST/SESSION data? (F5 / Ctrl+R keeps data?)
问题描述
我想创建一个按钮来重新加载页面而不会丢失 $ _ POST
数据和 $ _ SESSION
。
在网络上,我发现了这段代码:
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:
onclick="document.location.reload();"
以下是我的按钮代码:
<a class="button" href="" style="font-size: 0.7em; padding: 5px 10px;" onclick="document.location.reload();">Recharger la page</a>
但是当我点击按钮时,我输了 $ _ POST
数据和 $ _ SESSION
。
But when I click on the button, I lose $_POST
data and $_SESSION
.
如果我尝试使用键盘命令 Ctrl + R (Chrome)或 F5 (Firefox,IE9),浏览器显示警告通知我我再次尝试提交表单。如果我接受,它就有效。
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.
如何使用JavaScript命令重现这种浏览器刷新?或者我的按钮代码是错误的?
How can I reproduce this kind of browser-refresh with a JavaScript command? Or is the code of my button wrong?
非常感谢你的帮助。
推荐答案
尝试使用
location.reload(true);
这将执行硬刷新,不仅重建DOM而且还重新检索任何资源来自服务器。
This will perform a "hard" refresh, not just rebuilding the DOM but also re-retrieving any resource from the server.
显然, location.reload()
是在脚本中相当于 F5
,而 Ctrl + F5
/ Ctrl + R
可以使用 location.reload(true)来模拟
。
Apparently, location.reload()
is the equivalent of F5
in scripting, whilst Ctrl+F5
/ Ctrl+R
can be simulated using location.reload(true)
.
另外,正如ThiefMaster所提到的那样,你在 onclick
语句的末尾'缺少;返回false
,或者你应该设置 href
到 javascript:void 0
* 以阻止浏览器关注链接。
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.
*或任何其他返回未定义的JavaScript
这篇关于" location.reload()"丢失POST / SESSION数据? (F5 / Ctrl + R保存数据?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!