Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

javascript

When a user visits my website there is a "Login" link on every page. Clicking this uses some JavaScript to show an overlay window where the user is prompted for their credentials. After entering these credentials an Ajax call is made to the web server to validate them; if they are valid, an authentication ticket cookie is sent back and the page is reloaded so that any content on the page that is specific to authenticated users or the (now) currently logged in user is displayed.

I am accomplishing the page reload via script using:

window.location.reload(); 

This works wonderfully for pages loaded via a GET request (the vast majority), but some pages use postback forms. So if a user goes to one of these pages, performs a postback, and then chooses to login, when the window.location.reload() script runs they are prompted with the dialog box asking if they want to resubmit the POST body.

Resubmit POST body dialog box

I thought to get around this I could just tell the browser to reload the page, so I tried:

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

But the browser doesn't take any action with the above statement, I presume because it's thinking the new URL is the same as the old. If I change the above to:

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

It reloads the page, but I lose any querystring parameters.

My current workaround is sufficient, but not pretty - in short, I tack on a querystring parameter to the current window.location.href value and then assign that 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 results in the URL being www.example.com/SomeFolder/SomePage.aspx?justLoggedIn, which seems tacky.

Is there a way in JavaScript to get it to do a GET reload and not prompt the user to resubmit the POST data? I need to make sure that any existing querystring parameters are not lost.

like image 358
Scott Mitchell Avatar asked Feb 02 '11 00:02

Scott Mitchell


People also ask

How do I reload the current page without losing any form data?

The easiest way to reload the current page without losing form data, use WebStorage where you have -persistent storage (localStorage) or session-based (sessionStorage) which remains in memory until your web browser is closed. window. onload = function() { var name = localStorage.

How do I trigger a refresh page?

You can use the location. reload() JavaScript method to reload the current URL. This method functions similarly to the browser's Refresh button. The reload() method is the main method responsible for page reloading.

How do I refresh a page using anchor tag?

After you set the specified URL into location, issue window. location. reload(true) . This will force browser to reload the page.

What does Window location reload false do?

window. location. reload(true); False reloads the page using the version of the page cached by the browser.


1 Answers

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

Don't ask my why it works..but it does :).
Just the way the js engine interprets the logic I suppose.

like image 153
peaceslp Avatar answered Sep 20 '22 07:09

peaceslp