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.
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.
window.location.href = window.location.href + '&reload=1';
? – drudgereloadPage
function I note above), but it feels tacky. I'm hoping there's a more elegant solution. – Scott Mitchellwindow.location.href = window.location.href
worked for me (reload the page) under Firefox and Chromium. Maybe trywindow.location.replace(window.location.href)
(that additionally makes the page reload transparent to the user history) ... – Pierre