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.

Leave a Comment