最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - "location.reload()" loses POSTSESSION data? (F5Ctrl+R keeps data?) - Stack Overflow

programmeradmin0浏览0评论

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();"

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.

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.

How can I reproduce this kind of browser-refresh with a JavaScript command? Or is the code of my button wrong?

Thank you very much for your help.

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();"

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.

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.

How can I reproduce this kind of browser-refresh with a JavaScript command? Or is the code of my button wrong?

Thank you very much for your help.

Share Improve this question edited Jun 2, 2012 at 14:12 user2428118 8,1044 gold badges46 silver badges73 bronze badges asked Jun 2, 2012 at 9:22 ZorkzydZorkzyd 1,0293 gold badges13 silver badges34 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 9

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.

You can read more at the 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).

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.

*Or any other piece of JavaScript that returns undefined

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.

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.

The Ctrl + R refreshes the page and clears 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

Sources:
https://superuser.com/questions/205279/ctrlf5-vs-ctrlr-on-browsers
Browser issue in Ctrl-R

发布评论

评论列表(0)

  1. 暂无评论