• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

return false

 
Grega Leskovšek
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

https://developer.mozilla.org/en-US/docs/DOM/event.preventDefault
https://developer.mozilla.org/en-US/docs/DOM/event.stopPropagation
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65229
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
B. Katz
Ranch Hand
Posts: 53
Chrome Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings,

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.

Example:

HTH.

/me braces for Bear's corrections. ;)
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65229
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 65229
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  •  
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic