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

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


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 reloadedwindow.location.href = 'mysite.htm?r='+(+new Date())+'#images';


While I feel alemjerus's answer is on the good direction, it doesn't work when the new URL points to different page, at least on my Chrome v40.

Example, when the new URL just add an anchor, it works fine:

var redirectToURL = '/questions/2319004/javascript-how-to-truly-reload-a-site-with-an-anchor-tag#answer-28621360';window.location.href = redirectToURL;window.location.reload(true);

But when it points to a different page, user will still stay on the current page after reload. (Run the following codes in a batch, no delay between location.href= and location.reload):

var redirectToURL = 'http://stackoverflow.com';window.location.href = redirectToURL;window.location.reload(true);

In this case, remove the location.reload works. So my workaround is to check whether new URL is on the same page:

var urlParser = document.createElement('a');urlParser.href = redirectToURL;if (urlParser.origin + urlParser.pathname === location.origin + location.pathname) {    window.location.reload(true);}

Tested on Chrome and Safari, haven't tried IE. Kindly let me know if it doesn't work on any major browser.