Reload the browser window after POST without prompting the user to resend the POST data

When a user visits my site, each page has a "Login" link. By clicking on this, you use JavaScript to display the overlay window where the user is prompted for credentials. After entering these credentials on the web server, an Ajax call is called to verify them; if they are valid, the authentication cookie is sent back and the page reloads so that the content on the page that is specific to the authenticated users or the current user (now) is displayed.

I am reloading the page through a script using:

window.location.reload(); 

This works great for pages loaded via a GET request (the vast majority), but postback forms are used on some pages. Therefore, if the user goes to one of these pages, performs a postback, and then selects the login when the window.location.reload() script is started, they ask for a dialog box asking if they want to resend the POST body.

Resubmit POST body dialog box

I was thinking of getting around this, I could just tell the browser to reload the page, so I tried:

 window.location.href = window.location.href; 

But the browser does not take any action with the above statement, I suppose, because it thinks the new URL is the same as the old one. If I change the above, follow these steps:

 window.location.href = window.location.pathname; 

It reloads the page, but I am losing any request parameters.

My current workaround is enough, but not really - in short, I bind the querystring parameter to the current window.location.href value and then assign it back to window.location.href by calling reloadPage("justLoggedIn") , where the reloadPage function is:

 function reloadPage(querystringTackon) { var currentUrl = window.location.href; if (querystringTackon != null && querystringTackon.length > 0 && currentUrl.indexOf(querystringTackon) < 0) { if (currentUrl.indexOf("?") < 0) currentUrl += "?" + querystringTackon; else currentUrl += "&" + querystringTackon; } window.location.href = currentUrl; } 

This works, but it leads to the URL www.example.com/SomeFolder/SomePage.aspx?justLoggedIn , which seems sticky.

Is there a way in JavaScript to get it to restart the GET and not ask the user to re-send the POST data? I need to make sure that any existing query parameters are not lost.

+48
javascript
Feb 02 2018-11-11T00:
source share
12 answers
 window.location.href = window.location.href; 

Do not ask me why this works ... but it does :). Just how the js engine interprets the logic, I suppose.

+38
May 20 '11 at 21:35
source share

Perhaps you need to redirect the user after the POST / Form script handler has been started.

In PHP, this is done like this ...

 <?php // ... Handle $_POST data, maybe insert it into a database. // Ok, $_POST data has been handled, redirect the user header('Location:success.php'); die(); ?> 

... this should allow you to refresh the page without receiving the “Send data again” warning.

You can even redirect to the same page (if that's what you post), since POST variables will not be sent in the headers (and thus there will be no repeated POST when updating)

+8
Feb 02 '11 at 5:00
source share

This also works:

 window.location.href = window.location.pathname + window.location.search 
+7
Oct 31 '11 at 19:11
source share

Use

 RefreshForm.submit(); 

instead

 document.location.reload(true); 
+4
Apr 16 '13 at 13:21
source share

I had the same problem as you.

Here's what I did (a dynamically created GET form with an action set to location.href, hidden input with a fresh value), and it seems to work in all browsers:

 var elForm=document.createElement("form"); elForm.setAttribute("method", "get"); elForm.setAttribute("action", window.location.href); var elInputHidden=document.createElement("input"); elInputHidden.setAttribute("type", "hidden"); elInputHidden.setAttribute("name", "r"); elInputHidden.setAttribute("value", new Date().getTime()); elForm.appendChild(elInputHidden); if(window.location.href.indexOf("?")>=0) { var _arrNameValue; var strRequestVars=window.location.href.substr(window.location.href.indexOf("?")+1); var _arrRequestVariablePairs=strRequestVars.split("&"); for(var i=0; i<_arrRequestVariablePairs.length; i++) { _arrNameValue=_arrRequestVariablePairs[i].split("="); elInputHidden=document.createElement("input"); elInputHidden.setAttribute("type", "hidden"); elInputHidden.setAttribute("name", decodeURIComponent(_arrNameValue.shift())); elInputHidden.setAttribute("value", decodeURIComponent(_arrNameValue.join("="))); elForm.appendChild(elInputHidden); } } document.body.appendChild(elForm); elForm.submit(); 
+3
Mar 15 '13 at 23:13
source share

It worked for me.

window.location = window.location.pathname;

Checked for

  • Chrome 44.0.2403
  • IE edge
  • Firefox 39.0
+3
Aug 07 '15 at 5:56 on
source share

This is an older post, but I have a better solution. Create a form containing all your values ​​as hidden fields, and give the form a name, for example:

 <form name="RefreshForm" method="post" action="http://yoursite/yourscript"> <input type="hidden" name="postVariable" value="PostData"> </form> 

Then all you have to do in setTimeout is RefreshForm.submit();

Hurrah!

+2
Apr 21 2018-11-11T00:
source share

This can also be solved using the POST / REDIRECT / GET template.
Which is more elegant:
How to reload a page without warning POSTDATA in Javascript?

+2
Sep 08
source share

You can try to create an empty form, method = receive and submit it.

 <form id='reloader' method='get' action="enter url here"> </form> <script> // to reload the page, try document.getElementById('reloader').submit(); </script> 
+1
Apr 21 2018-11-11T00:
source share

To reload the window that opens with all the parameters (not all must be sent using the window.location.href method and the method with the form .submit (), perhaps one step to the end), I prefer to use the window.history method.

 window.opener.history.go(0); 
0
Apr 09 '14 at 13:54 on
source share

If you have "#" hashes in your URL, all solutions here do not work. This is the only solution that worked for me.

 var hrefa = window.location.href.split("#")[1]; var hrefb = window.location.href.split("#")[2]; window.location.href = window.location.pathname + window.location.search + '&x=x#' + hrefa + '#' + hrefb; 

The url must be different to reload if you have a hash. Here & x = x does this. Just replace this with something you can ignore. This is absurdity from hacking, unable to find a better solution.

Tested in Firefox.

0
Jun 12 '15 at 18:32
source share

You can use the HTML hint for unloading by specifying the unload handler:

 window.onbeforeunload = null; window.location.replace(URL); 

See the comments section of WindowEventHandlers.onbeforeunload .

0
Oct 17 '17 at 15:50
source share