I want to reload a page using:
window.location.reload(true);
But I receive the POSTDATA warning because the refresh function want to resend previous POST form data. How can I refresh my page without this warning?
UPDATED: I have no control of the project! I can't workaround the POST itself!
You can use the location. reload() JavaScript method to reload the current URL. This method functions similarly to the browser's Refresh button. The reload() method is the main method responsible for page reloading.
After you set the specified URL into location, issue window. location. reload(true) . This will force browser to reload the page.
You can't refresh without the warning; refresh instructs the browser to repeat the last action. It is up to the browser to choose whether to warn the user if repeating the last action involves resubmitting data.
You could re-navigate to the same page with a fresh session by doing:
window.location = window.location.href;
Just changing window.location
in JavaScript is dangerous because the user could still hit the back button and resubmit the post, which could have unexpected results (such as a duplicate purchase). PRG is a much better solution
Use the Post/Redirect/Get (PRG) pattern
To avoid this problem, many web applications use the PRG pattern — instead of returning an HTML page directly, the POST operation returns a redirection command (using the HTTP 303 response code (sometimes 302) together with the HTTP "Location" response header), instructing the browser to load a different page using an HTTP GET request. The result page can then safely be bookmarked or reloaded without unexpected side effects.
If you want to do it entirely client side, you'll need to change the browser history before you do the refresh:
if ( window.history.replaceState ) { window.history.replaceState( null, null, window.location.href ); } window.location = window.location.href;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With