2

How do I force Javascript to refresh onload only once? My take:

$(window).ready(function(res){
    var hash = '#refreshed';
    if(window.location.hash != hash) window.location.replace(window.location + hash)
});

But this doesn't refresh the page. Any ideas why?

0

2 Answers 2

2

The purpose of a hash is that changing it won't reload the page.

As of that you would need to use MDN: Location.reload() after you changed the hash.

Sign up to request clarification or add additional context in comments.

Comments

2

Have you tried using location.reload?

The Location.reload() method Reloads the resource from the current URL. Its optional unique parameter is a Boolean, which, when it is true, causes the page to always be reloaded from the server. If it is false or not specified, the browser may reload the page from its cache.

Example:

$(window).ready(function(res){
  var hash = '#refreshed';
  if(window.location.hash != hash) {
    window.location.replace(window.location + hash); // add your hash
    document.location.reload();                      // reload the page
  }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.