Reputation: 9949
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!
Upvotes: 80
Views: 170968
Reputation: 12627
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;
Upvotes: 73
Reputation: 1248
you are not forced to use javascript to do every thing. Many problems have easy solutions like this one. you can use this tricky anchor:
<a href=".">Continue</a>
of course I know you may need an automatic solution but this will help in many cases.
good luck
Upvotes: 0
Reputation: 8902
The other solutions with window.location
didn't work for me since they didn't make it refresh at all, so what I did was that I used an empty form to pass new and empty postdata to the same page. This is a way to do that based on this answer:
function refreshAndClearPost() {
var form = document.createElement("form");
form.method = "POST";
form.action = location.href;
form.style.display = "none";
document.body.appendChild(form);
form.submit(); //since the form is empty, it will pass empty postdata
document.body.removeChild(form);
}
Upvotes: -1
Reputation: 836
Here's a solution that should always work and doesn't remove the hash.
let currentPage = new URL(window.location.href);
currentPage.searchParams.set('r', (+new Date * Math.random()).toString(36).substring(0, 5));
window.location.href = currentPage.href;
Upvotes: -1
Reputation: 1
using meta refresh in html
<meta http-equiv='refresh' content='1'>
using meta refresh in php
echo "<meta http-equiv='refresh' content='1'>"
Upvotes: -2
Reputation:
If you use GET
method instead of POST
then we can't the form filed values. If you use window.opener.location.href = window.opener.location.href;
then we can fire the db and we can get the value but only thing is the JSP
is not refreshing eventhough the scriplet having the form values.
Upvotes: -2
Reputation: 61
This worked
<button onclick="window.location.href=window.location.href; return false;">Continue</button>
The reason it didn't work without the
return false;is that previously it treated that as a form submit button. With an explicit return false on it, it doesn't do the form submit and just does the reload of the same page that was a result of a previous POST to that page.
Upvotes: 4
Reputation: 12619
I've written a function that will reload the page without post submission and it will work with hashes, too.
I do this by adding / modifying a GET parameter in the URL called reload
by updating its value with the current timestamp in ms.
var reload = function () {
var regex = new RegExp("([?;&])reload[^&;]*[;&]?");
var query = window.location.href.split('#')[0].replace(regex, "$1").replace(/&$/, '');
window.location.href =
(window.location.href.indexOf('?') < 0 ? "?" : query + (query.slice(-1) != "?" ? "&" : ""))
+ "reload=" + new Date().getTime() + window.location.hash;
};
Keep in mind, if you want to trigger this function in a href attribute, implement it this way: href="javascript:reload();void 0;"
to make it work, successfully.
The downside of my solution is it will change the URL, so this "reload" is not a real reload, instead it's a load with a different query. Still, it could fit your needs like it does for me.
Upvotes: 0
Reputation: 1661
Nikl's version doesn't pass get query parameters, I used the following modified version:
window.location.href = window.location.protocol +'//'+ window.location.host + window.location.pathname + window.location.search;
or in my case I needed to refresh the topmost page\frame, so I used the following version
window.top.location.href = window.top.location.protocol +'//'+ window.top.location.host + window.top.location.pathname + window.top.location.search;
Upvotes: 3
Reputation: 5481
I had some problems with anchor/hash-urls (including #) not reloading using the solution from Rex...
So I finally ended up by removing the hash part:
window.location = window.location.href.split("#")[0];
Upvotes: 24
Reputation: 81
You can use JavaScript:
window.location.assign(document.URL);
Worked for me on Firefox and Chrome
check this link: http://www.codeproject.com/Tips/433399/PRG-Pattern-Post-Redirect-Get
Upvotes: 8
Reputation: 19
<html:form name="Form" type="abc" action="abc.do" method="get" onsubmit="return false;">
method="get"
- resolves the problem.
if method="post"
then only warning comes.
Upvotes: 1
Reputation: 1104
To bypass POST warning you must reload page with full URL. Works fine.
window.location.href = window.location.protocol +'//'+ window.location.host + window.location.pathname;
Upvotes: 13
Reputation: 32490
If you are at the stage where you are finished with the post data and simply want to view the page again afresh, you could just use a window.location and even maybe append a random string as a query paramater to guarantee a new version of the page.
Upvotes: 2
Reputation: 144162
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;
Upvotes: 149