File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes HTML, CSS and JavaScript and the fly likes return false Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "return false" Watch "return false" New topic

return false

Grega Leskovšek
Ranch Hand

Joined: Nov 13, 2008
Posts: 76
I am not clear when to write at the end of an action like onclick="javascript code; return false;"
return false?
Thanks in advance, Grega

--Always, Grega from Peace refuge
Eric Pascarello

Joined: Nov 08, 2001
Posts: 15385
return false cancels the default action. So if you need to cancel the default action you use it.

You should look at the event model
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63844

THis is just one reason that embedding behavior in HTML markup is a bad idea. It masks what's really going on.

The value of the attribute becomes the body of an anonymous function. Hence the last "statement" returns the value false form this function.

You should look into the concept of Unobtrusive JavaScript.

[Asking smart questions] [About Bear] [Books by Bear]
B. Katz
Ranch Hand

Joined: Aug 28, 2009
Posts: 52


If you don't mind me adding my 2 ¢ents here, one good example of where I think you can use "return false;" effectively is to kill the context menu (a.k.a. right-click) of a page when you don't want the users playing around that much.



/me braces for Bear's corrections. ;)

"I do not feel obliged to believe that the same God who has endowed us with sense, reason, and intellect has intended us to forgo their use."
-- Galileo Galilei
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63844

Sorry but disabling the context menu is a silly thing to do. What purpose could it possibly really serve? Anything that can be done via the context menu can be done by other means that cannot be blocked. So other than to annoy your users, what's the reason?
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63844

B. Katz wrote:/me braces for Bear's corrections. ;)

And not to disappoint:
  • Again, avoid embedding behavior in structure.
  • Don't use all uppercase for HTML. It makes it look like it's from 1998.
    I agree. Here's the link:
    subject: return false
    It's not a secret anymore!