76

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!

Sam Hasler
  • 12,344
  • 10
  • 72
  • 106
Ricibald
  • 9,369
  • 7
  • 47
  • 62
  • 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. – Sam Hasler Feb 20 '09 at 16:39
  • 1
    Duplicate: http://stackoverflow.com/questions/1073593/php-reload-page-without-posting-data/1073732#1073732 – Quentin Jul 03 '09 at 09:18
  • 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. – Jiří Mar 29 '22 at 15:42

17 Answers17

144

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
  • 142,167
  • 33
  • 283
  • 313
  • 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 Feb 20 '09 at 15:48
  • 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 Feb 20 '09 at 16:01
  • 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 Feb 20 '09 at 16:04
  • 3
    @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 Feb 20 '09 at 18:36
  • 1
    Note that this will not work (at least in firefox) when the url contains a #. – Leven Jul 18 '13 at 11:25
  • 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. – Martin Braun Nov 08 '16 at 13:28
  • It didn't work for me, it didn't reload the page at all. – Donald Duck Oct 28 '18 at 17:08
  • 1
    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!!! – tom Nov 02 '20 at 08:42
  • 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 - – MJHd May 11 '21 at 14:08
71

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;
Sam Hasler
  • 12,344
  • 10
  • 72
  • 106
  • I have no control of the project. I can't workaround the POST itself! – Ricibald Feb 20 '09 at 16:21
  • 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 Feb 20 '09 at 16:31
  • 4
    Yes, but redirecting a POST require some form of control – Ricibald Feb 20 '09 at 17:26
  • 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 code – Gina Gina Jul 24 '18 at 06:10
  • 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). – flaviovs Jan 23 '22 at 06:44
  • @flaviovs, thanks for pointing that out. I've added a client side solution using the history API – Sam Hasler Jan 24 '22 at 10:12
  • 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 – anishsane May 27 '22 at 04:30
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];
Rob
  • 5,353
  • 33
  • 34
  • 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. – anishsane May 27 '22 at 04:38
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;
Nikl
  • 1,064
  • 10
  • 10
  • This does not work. Although it does bypass POST warning, it also bypasses sending any post variables that might be required. – iankit Jan 17 '14 at 15:22
  • Add window.location.hash to the end then. – Ian Stanway Jul 09 '15 at 15:39
  • For a full URL with protocol (if not 80) and parameters, see http://bl.ocks.org/abernier/3070589. – Ian Stanway Jul 09 '15 at 15:46
  • 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; – Patlatus Jan 06 '16 at 16:20
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

eebbesen
  • 5,070
  • 8
  • 48
  • 70
Dan
  • 81
  • 1
  • 2
5

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

david
  • 51
  • 1
  • 1
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.
Prasanna
  • 61
  • 1
  • 7
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;
Patlatus
  • 1,217
  • 2
  • 16
  • 28
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.

AJM
  • 32,054
  • 48
  • 155
  • 243
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.

Rajendra arora
  • 2,186
  • 1
  • 16
  • 23
ABC
  • 19
  • 1
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.

Martin Braun
  • 10,906
  • 9
  • 64
  • 105
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

mahyard
  • 1,230
  • 1
  • 13
  • 34
0

just use

location.reload(true);

without window.

Orlyyn
  • 2,296
  • 2
  • 20
  • 32
-1

using meta refresh in html

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

using meta refresh in php

echo "<meta http-equiv='refresh' content='1'>"
rafif
  • 1
  • 2
-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;
Bjørnar Hagen
  • 757
  • 1
  • 8
  • 19
-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);
}
Donald Duck
  • 8,409
  • 22
  • 75
  • 99
-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.

Nick is tired
  • 6,860
  • 20
  • 39
  • 51