One Time Page Refresh After First Page Load

Refresh/Reload web page only once

You should keep a flag in cookie, server session, or local storage. For example:

window.onload = function () {
if (! localStorage.justOnce) {
localStorage.setItem("justOnce", "true");
window.location.reload();
}
}

Reload an HTML page just once using JavaScript

You could use a querystring at the end of the page url (e.g. ?r), and check for it before redirecting, as if it exists, the redirect is already done.

if(window.location.href.substr(-2) !== "?r") {
window.location = window.location.href + "?r";
}

Disclaimer: I agree with others that you probably have a better solution - and should never need to refresh 'just once'.

Explanation of use

At the bottom of your page, just above the </body> you should put this:-

<script type="text/javascript">
if(window.location.href.substr(-2) !== "?r") {
window.location = window.location.href + "?r";
}
</script>

That's it.



Related Topics



Leave a reply



Submit