Javascript: How to truly reload a site with an anchor tag?

Viewed 53122

Take this code snippet for example:

window.location.href = 'mysite.htm#images';

Already being on the site mysite.htm and triggering the reload thru location.href won't reload the page but jump to the anchor named within the URL.

I need a mechanism to truly reload the page. Any ideas?

PS: I tried location's other methods but none of them does the trick :(

EDIT
Don't hang on the filetype htm. I'd need it for dynamic sites as well.

7 Answers

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

use the query part of the URL to add a random string:

// Use a timestamp to make sure the page is always reloaded
window.location.href = 'mysite.htm?r='+(+new Date())+'#images';

You could be a cheater and append a randomized query string, redirecting to "mysite.htm?random=289954034#images".

But if this is a static page, as .htm implies, what do you need reloading for? If it's so that your onload Javascript can occur a second time, maybe you just need to change your application's flow.

I found that the answer by @alemjerus would retrigger a POST if that was the last request:

window.location.reload(true)

What I wanted was to refresh the page from scratch using a GET, so I used this solution:

<a href="javascript:window.location.href=window.location.href">Refresh page</a>

Try this: window.location.href = 'mysite.htm?';

EDIT: This will only work the first time; after that, the caching takes effect again.

Related