stopping default action using "return false" not recommended?

Discussion in 'JavaScript' started by winterheat, Aug 25, 2008.

  1. #1
    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?
     
    winterheat, Aug 25, 2008 IP
  2. Wrighty

    Wrighty Peon

    Messages:
    199
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    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! :-/
     
    Wrighty, Aug 25, 2008 IP
  3. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #3
    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.
     
    joebert, Aug 29, 2008 IP