How to Reload a Page Only Once Using Javascript

By | July 7, 2008

Here is a small piece of code that allows to reload a page only once. This is necessary to prevent some data being cached and this is the only solution I found good for my needs. Meta tag nocache did not save me, and meta tag refresh does not allow to refresh the page only once. So here is the code:

<script>
var reloaded = false;
var loc=””+document.location;
loc = loc.indexOf(“?reloaded=”)!=-1?loc.substring(loc.indexOf(“?reloaded=”)+10,loc.length):””;
loc = loc.indexOf(“&”)!=-1?loc.substring(0,loc.indexOf(“&”)):loc;
reloaded = loc!=””?(loc==”true”):reloaded;

function reloadOnceOnly() {
if (!reloaded)
window.location.replace(window.location+”?reloaded=true”);
}
reloadOnceOnly();
</script>

I found this in Google, but I had to spend some hours before I did it. So I think this article will be helpful for you.