2
votes

I have a JavaScript app where the method location.reload() is used. The method location.reload() is in a onclick event handler to reset the page, and this answer says that you need to return false; after location.reload() with onclick : How to reload a page using JavaScript.

location.reload();

See this MDN page for more information.

If you are refreshing after an onclick then you'll need to return false directly after

location.reload();
return false;

Why should you return false; after method location.reload() using onclick?

1
Your question is completely taken out of context; why not just add the method?Martin Zeitler
I'm guessing wildly here, but could it be in regards to <a> elements or form submission?Chloe Anderson
That question is pretty old (> 10 years). It seems the parameter for reload() - a true/false to indicate whether the browser should force a refresh - has since been deprecated. I imagine now that it's just the browser that makes a decision about the type of refresh / cache call that is done when instructed to "reload()". Obviously, the parameter can still be passed and doesn't specifically raise an error - but it's definitely not actually doing anything, to the best of my knowledge.Craig
@Craig - the question is a bit ambiguous, but I believe the poster is referring to this answer that explicitly suggests return false after the reload is necessary. I also thought they were talking about the answer of passing false to .reload at first glance. @FilipHuhta - for what it's worth, there are a few comments challenging/questioning this assertion regarding returning false on that answer.Alexander Nied
If you're just expecting the screen to refresh, and don't have anything following the "location.reload()" call that's either expecting a result, or performing any further functions, then in essence you shouldn't need anything extra. If you do have either nested function calls, though, or subsequent code (that you don't want to run, in the case of needing to reload), then returning a false might make sense (if your subsequent code expects a boolean result). I certainly don't believe there's anything that will inherently affect the browser behaviour, whether there's a "return" value or notCraig

1 Answers

5
votes

If the event listener is attached to a link, then clicking the link will result in going to another page instead of reloading the page. return false will prevent the default action in an inline event handler and the onclick property.

Without return false:

document.querySelector('a').onclick = function() {
  location.reload();
}
<a href="https://www.example.com">Click</a>

With return false:

console.log('Loaded', new Date);
document.querySelector('a').onclick = function() {
  location.reload();
  return false;
}
<a href="https://www.example.com">Click</a>

In modern JavaScript, addEventListener and event.preventDefault() would be used instead.

document.querySelector('a').addEventListener('click', function(e){
    e.preventDefault();
    location.reload();
});