views:

5035

answers:

4

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!

+9  A: 

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;
Rex M
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.
Sam Hasler
Maybe, but if the user clicks back for whatever reason they will still get a warning, so perhaps using PRG depends on what the consequences of resubmission are.
AJM
true, if the server recognises duplicate posts it's not a problem, but the user is still presented with a dialog to worry about. With PRG the user will never see that dialog.
Sam Hasler
@sam you've gone outside the scope of the question. we don't know anything about how or why the asker is in the situation he's in.
Rex M
+2  A: 

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.

AJM
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.
Sam Hasler
+7  A: 

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.

Sam Hasler
I have no control of the project. I can't workaround the POST itself!
Ricibald
PRG is done after the POST, silently redirecting the client to a GET page so they don't have the POST in their history. (It's worth bringing up with whoever does have control.) If that's something you're not able to do then can you navigate to a GET page instead of reloading the POST?
Sam Hasler
Yes, but redirecting a POST require some form of control
Ricibald
A: 

hi 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

Regards Palani