How do I reload a page without a POSTDATA warning in Javascript?
Asked Answered
P

17

78

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!

Pix answered 20/2, 2009 at 15:35 Comment(3)
resubmitting a POST (which is what a reload does) will always bring up that warning. You could do another POST instead of reloading, although that would fill up the users browsers history if you do it too often, and they'd still get the warning if they hit the back button.Willingham
Duplicate: #1074093Coffee
Not a duplicate, here they want to avoid the POST resubmit warn, not necessarily the action itself. More then years later, still struggling. I WANT to resend the POST, without bothering the user.Natika
W
73

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.

Client Side

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;
Willingham answered 20/2, 2009 at 15:46 Comment(7)
I have no control of the project. I can't workaround the POST itself!Pix
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?Willingham
Yes, but redirecting a POST require some form of controlPix
How can we use PRG, I still dont get the idea, when I reload a page , it always asks me whether I want to reload, how to apply PRG here, can you please provide a sample codeAurelio
PRG is a serve-side solution. OP is asking about a front-end (client) solution. One does not necessarily always control the software in the server (for example, when creating userscripts).Retention
@flaviovs, thanks for pointing that out. I've added a client side solution using the history APIWillingham
PRG is cool, but then I wish to display messages like "Data saved successfully" or "failed to dave the data" for postdata. Triggering a separate request is overhead (I can retain the post-data in form of cookie or GET querystring, but still, extra overhead). But the client-side option works great for me! I simply added it in the <script> tag of the received page. Thank you!Refuse
E
146

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;
Entertain answered 20/2, 2009 at 15:38 Comment(9)
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.Willingham
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.Fuller
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.Willingham
@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.Entertain
Note that this will not work (at least in firefox) when the url contains a #.Hulk
If you want this action on a href attribute, remember to add void 0; at the end, so: href="javascript:window.location = window.location.href;void 0;" or you will end up by getting a white page with only the href printed out.Redbreast
It didn't work for me, it didn't reload the page at all.Rambouillet
the policing on this site is so, so naïve. I have been writing this stuff (a.k.a. code) for 36 years. The way the subject is handled (in this answer) is not only perfect, it shows sensitive insight into the potential complexities of the user's issue. And google ties it all together with their search engine. Thank you!!!Codpiece
It depends on the application - we have to trust developers to make smart choices. In my case it's opening and closing a PDF by POST - so I really don't care if my user want's to cycle back to the main interface using back - everything is application dependent -Karmenkarna
W
73

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.

Client Side

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;
Willingham answered 20/2, 2009 at 15:46 Comment(7)
I have no control of the project. I can't workaround the POST itself!Pix
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?Willingham
Yes, but redirecting a POST require some form of controlPix
How can we use PRG, I still dont get the idea, when I reload a page , it always asks me whether I want to reload, how to apply PRG here, can you please provide a sample codeAurelio
PRG is a serve-side solution. OP is asking about a front-end (client) solution. One does not necessarily always control the software in the server (for example, when creating userscripts).Retention
@flaviovs, thanks for pointing that out. I've added a client side solution using the history APIWillingham
PRG is cool, but then I wish to display messages like "Data saved successfully" or "failed to dave the data" for postdata. Triggering a separate request is overhead (I can retain the post-data in form of cookie or GET querystring, but still, extra overhead). But the client-side option works great for me! I simply added it in the <script> tag of the received page. Thank you!Refuse
I
24

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];
Ilona answered 22/10, 2014 at 10:46 Comment(1)
I tend to add/replace &unused_param=1/&unused_param=0 in the querystring. Most of the times, such parameter is not used in the actual form data. And since the querystring is changing, browser triggers a re-fetch of the page. Obviously this unused parameter's value needs to be changed every time. If the before/after values are same, it will not trigger the reload.Refuse
D
13

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;
Devonne answered 19/2, 2013 at 8:59 Comment(4)
This does not work. Although it does bypass POST warning, it also bypasses sending any post variables that might be required.Delogu
Add window.location.hash to the end then.Neille
For a full URL with protocol (if not 80) and parameters, see bl.ocks.org/abernier/3070589.Neille
this 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;Homeric
P
8

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

Patman answered 28/5, 2014 at 14:41 Comment(0)
P
5

how about window.location.replace(window.location.href);

Protocol answered 19/11, 2010 at 20:36 Comment(0)
M
4

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.
Madgemadhouse answered 22/1, 2018 at 16:9 Comment(0)
H
3

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;
Homeric answered 6/1, 2016 at 16:34 Comment(0)
F
2

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.

Fuller answered 20/2, 2009 at 15:39 Comment(0)
T
1
<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.

Transpicuous answered 11/2, 2014 at 4:28 Comment(0)
R
0

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.

Redbreast answered 8/11, 2016 at 14:6 Comment(0)
E
0

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

Ellata answered 25/4, 2019 at 7:15 Comment(0)
B
-1

using meta refresh in html

<meta http-equiv='refresh' content='1'>

using meta refresh in php

echo "<meta http-equiv='refresh' content='1'>"
Bedwarmer answered 14/5, 2018 at 14:43 Comment(0)
D
-1

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;
Domenicadomenico answered 15/10, 2018 at 20:16 Comment(0)
R
-1

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);
}
Rambouillet answered 29/10, 2018 at 10:49 Comment(0)
F
-1

just use

location.reload(true);

without window.

Finochio answered 17/6, 2021 at 20:28 Comment(0)
E
-2

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.

Exemplification answered 3/7, 2009 at 9:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.