Why Javascript location.reload does not work?

Posted in Articles

Tweet This Share on Facebook Bookmark on Delicious Digg this Submit to Reddit

w3schools.com says that to reload a page, you use Javascript like this …

location.reload();

And according to MDN, it works in all modern browser.  But in rare situations, it doesn’t seem to work.  I found myself with that problem today.

It could be that the reload somehow reloaded the cached version of the page.  In that case, you can force it to reload the page from the server …

location.reload(true);

Or try adding document in front…

document.location.reload(true);

Other people seem to have encounter such scenarios and suggested some work-arounds on StackOverflow including …

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

(which ended up working for my situation).  But some say it doesn’t work if you have a hash (#) in the href.

You can go further by adding cache busting technique by appending a date timestamp in the querystring of the href.

Or …

history.go(0);

which is the one way that they forgot when they listed the 535 ways to reload a page in Javascript.

You can use the following to test your reload ways…

test-reload