Find jobs - Myspace Layouts - Online Advertising - Air Jordan Release Date - Debt Consolidation

PDA

View Full Version : stopping default action using "return false" not recommended?


winterheat
Aug 25th 2008, 4:51 am
i think conventional usage of Javascript is to use "return false" to stop the default action, be it a form for submit, a link, or a text input field.

when nothing is returned or when true is returned, then the default action is taken.

example like on http://www.0011.com/test/test_return_false.html

however, i found that this is not so widely mentioned or documented in some books. such as in Professional Javascript by Nicholas Zakas (2005) p. 278, he recommends using

oEvent.returnValue = false; // for IE

and

oEvent.preventDefault(); // for Firefox

(i don't have a copy of Javascript Definitive Guide with me right now... so I wonder if it documents returning false and true or not).

why not just use return false or true to do that? won't that work in almost all browsers?

Wrighty
Aug 25th 2008, 9:11 am
As far as I know return false works in all Browsers. It's standards. As for coding for IE & FF - I'm not one to do that, I prefer to code DOM and thus it should be CB instead of adding an extra line. I would continue using return false if I were you! :-/

joebert
Aug 29th 2008, 9:02 pm
If you don't have multiple event listeners, (addEventListener/attachEvent, not onEvent) some of which should not execute depending on the outcome of others, then "return false" is fine.