One time page refresh after first page load

Martin picture Martin · Aug 8, 2011 · Viewed 334k times · Source

I would like to implement a JavaScript code which states this: if the page is loaded completely, refresh the page immediately, but only once. I'm stuck at the "only once":

window.onload = function () {window.location.reload()}

this gives a loop without the "only once". jQuery is loaded if this helps.

Answer

Cokegod picture Cokegod · Aug 8, 2011

I'd say use hash, like this:

window.onload = function() {
    if(!window.location.hash) {
        window.location = window.location + '#loaded';
        window.location.reload();
    }
}