6

I'm making a JavaScript app where I use the method location.reload(). 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?

Filip Huhta
  • 2,043
  • 7
  • 25
  • 1
    Your question is completely taken out of context; why not just add the method? – Martin Zeitler Apr 30 '21 at 04:45
  • I'm guessing wildly here, but _could_ it be in regards to `` elements or form submission? – Nora Apr 30 '21 at 04:46
  • 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 Apr 30 '21 at 04:48
  • @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](https://stackoverflow.com/questions/3715047/how-to-reload-a-page-using-javascript#3715053). 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 Apr 30 '21 at 04:51
  • Oh - my bad. I did misinterpret the question – Craig Apr 30 '21 at 04:51
  • I've looked it up and there doesn't seem to be such a requirement; at least not for `location.reload()`: https://www.w3.org/TR/html52/browsers.html#dom-location-reload – Martin Zeitler Apr 30 '21 at 04:53
  • @AlexanderNied I saw the comments and I can't see in which case I should use the return false. But I don't want to remove something that maybe could be necessary to have... – Filip Huhta Apr 30 '21 at 04:58
  • 2
    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 not – Craig Apr 30 '21 at 04:59
  • @MartinZeitler I also looked up the dom doc and I did not found anything about it. – Filip Huhta Apr 30 '21 at 04:59

1 Answers1

15

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();
});
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
  • 4
    This is a fair enough example of when returning false would be applicable. – Craig Apr 30 '21 at 05:00
  • you mean `event.preventDefault()`? It's not 1998 anymore, we haven't had to return `false` for a very, very long time now. Also, as it's not 1998 anymore: don't use `onclick`, use a normal event handler. – Mike 'Pomax' Kamermans Apr 30 '21 at 05:02
  • 3
    @Mike'Pomax'Kamermans I am aware of that. The example just serves to explain why `return false` was suggested. – Unmitigated Apr 30 '21 at 05:02
  • Presumably because "the tutorial was ancient". Lots of tutorials still teaching completely out of date practices =( (but we can help fix that telling people that what they're asking about is ancient in terms of JS practice, and help them learn the modern way to do things) – Mike 'Pomax' Kamermans Apr 30 '21 at 05:03
  • If the `href` would be the usual `#`, the `return false` statement may never be reached. – Martin Zeitler Apr 30 '21 at 05:10
  • @MartinZeitler I don't see what you mean. – Unmitigated Apr 30 '21 at 05:12
  • 2
    This is a great example of a situation in which `return false` _would_ be necessary. I do want to point out, though, that you _probably_ shouldn't be putting click handlers that refresh the page on `a` tags with an `href`; there's probably very few scenarios in which that would be indicated. So, the scenario is a great example, but also, if you're encountering this exact scenario consider it a code smell. – Alexander Nied Apr 30 '21 at 13:31
  • 1
    Thank you for your example, In my case it would not be necessary to use `return: false;` but it is good to know when to use it. – Filip Huhta May 04 '21 at 08:11