Using windows.location.replace to refresh page not working with a hash in URL
Did you try with:
window.location.hash = ''; //if you want to reload with an empty hash
window.location.reload(true); //reload the page and bypass the cache
We use a single web page for our web app, and some functionality(change in currency, language,...) could trigger a reload.
If you want to prevent some re-post you can use a cookie to prevent the server doing double work.
Peter
Updated on June 04, 2022Comments
-
Peter about 2 years
I have an AJAX call which takes care of some server side settings (I'm using this for login, language switches, etc.). If, and only if, server side settings are actually changed as a result of this call, I want to refresh the current page (without reposting POST form data, should we be on a page right after a POST). A simple JS in the callback of the AJAX takes care of this:
window.location.replace( window.location.toString() );
This worked fine, until I started working with anchors. Let's say my URL is something like http://www.mysite.com/index/list#someplace and I do the aforementioned ajax call ending with the
window.location.replace
, then nothing happens. The page does not get reloaded. So far tested on FF3.6 and IE7. -
Peter over 13 yearsI'm trying to do something similar. Not one web page, but I'm trying to condense the app I'm writing in only a few pages. It's exactly the same things (currency, language, a log in) that will trigger a reload of the page. However, if I destroy the hash I risk that the user ends up looking a completely different page as the hashes, at the moment, play in important role in de layout (jquery tools tabs history).
-
Peter over 13 yearsPressed, enter to quickly, what I wanted to add is, that I don't know how to use a cookie to prevent a re-post and the re-post alert but I'll look into that, thanks for the tip!
-
Mic over 13 yearsIf you don't use
window.location.hash = ''
the hash key remains when you reload. -
Peter over 13 yearsHey Mic, sorry for the slow response, been working on some other stuff. Anyway, but what if I need the hash to stay in place? I currently solved it by using jQuery UI tabs with cookie history instead of hash based history. Which is not a solution at all, only a work around. The problem will pop up again later on in the project for sure.
-
Peter over 13 yearsAnd forgot to mention, even if I follow the proposed code, with a hash in the URL, it still doesn't do the reload. Anyways, thanks for the help!
-
Mic over 13 yearsThe natural pattern, is if you use a hash, you don't reload the page, just hide and show some
DIV
. May be late for your project, but that could workaround your problem, as there wouldn't be reloads anymore. Good luck. -
HardlyNoticeable over 6 yearsI didn't know about window.location.hash. Very handy.