Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"location.reload()" loses POST/SESSION data? (F5 / Ctrl+R keeps data?)

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.

like image 891
Zorkzyd Avatar asked Jun 02 '12 09:06

Zorkzyd


People also ask

What is location reload ()?

The location. reload() method reloads the current URL, like the Refresh button.

Does Ctrl F5 Clear session storage?

Does Ctrl F5 Clear Session Storage? The cache is not deletered by Ctrl F5, Shift F5 or another command, but it is ignored. If you want to clear the browser's cache, open the option via the shortcut Ctrl Shift Delete (or Ctrl Shift Del).

What is the difference between location reload () and window location reload ()?

location. reload(true); reloads the page from the server instead of from the cache, window. location. reload(); would do the same thing as location.

How does window location reload work?

The reload() method is used to reload the current document. The reload() method does the same as the reload button in your browser. By default, the reload() method reloads the page from the cache, but you can force it to reload the page from the server by setting the forceGet parameter to true: location. reload(true).


1 Answers

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

like image 106
user2428118 Avatar answered Nov 12 '22 23:11

user2428118