Jun 1, 2013
kalpesh

return false vs preventDefault in javascript

This post will help you to differentiate between javascript’s return false, preventDefault, stopPropagation and stopImmediatePropagation.

The first thing you learn when using javascript is to use return false. That’s the best thing we think to cancel browser’s default behavior, to stop further execution of an event. If you are using a simple function which returns what you want, then probably you won’t notice anything wrong in using return false. But when you want event to propagate, return false will not be helpful and will give unexpected result.

What is event propagation?

When a single event, such as a mouse click, if is handled by two or more event handlers defined in various location of the DOM hiearachy, then the event handling starts by executing for individual elements at the lowest level. There are two models of this event propagation, event bubbling and event capturing.

Event bubbling is when an event has more than one event handler and event handling execution starts from bottom to top i.e. from individual links to it’s parent element (e.g. form or body or document) having it’s own event handler.

1
2
3
4
5
6
7
8
9
<html>
  <body onclick="3rdEvent()">
    <form onclick="2ndEvent()">
      <a href="#" onclick="1stEvent()">
        This is a link!
      </a>
    </form>
  </body>
</html>

On clicking on the link in the above example code, first the hyperlink’s event will be handled, then form’s event will get handled, and at last body’s event will get handled. This is event bubbling, which bubble up from bottom to top.

Event capturing is opposite to event bubbling, where event handling execution starts from top to bottom.

If you have such type of architecture, where there are many event handlers for an event, you should not use return false or atleast use it with caution. It can get you in big trouble, as it does preventDefault() and stopPropagation() both and hence never allows further execution of an event.

preventDefault()

It simply stops the default action of an element. Example, prevents the hyperlink from following the URL, prevents the submit button to submit the form. When you have many event handlers and you just want to prevent default event from occuring, just use e.preventDefault() in the top of the function definition. The reason we put preventDefault() in the first line of function is because, if something goes wrong in the code following the preventDefault(), then also it will not allow link or form to get submitted or do whatever action you want it to do. And hence the link or submit button will get submitted. It will still allow further propagation of the event.

Following example prevents the link from following the URL given in href attribute.

1
2
3
4
5
6
7
8
9
10
11
12
<html>
    <body>
    <div>
      <a href="http://ka.lpe.sh/" onclick="doSomethingElse()">Blah</a>
    </div>
    <script type="text/javascript">
      $("div a").click(function(e){
            e.preventDefault();
            });
     </script>
    </body>
</html>

stopPropagation()

1
2
3
4
5
6
7
<html>
    <body>
    <div onclick="doSomething()">
      <a href="#" onclick="doSomethingElse()">Blah</a>
    </div>
    </body>
</html>

The above code has two event handlers for hyperlink. If you click on Blah, doSomethingElse() and then doSomething() will get fired.
Now let’s say you want to stop the execution of div’s onclick function i.e. doSomething(), but you want the anchor tag’s onclick to fire when clicking on Blah, then use stopPropagation().

1
2
3
4
$("div a").click(function (e) {  
    alert('Link is clicked! Sorry div will not get clicked..');
    e.stopPropagation();
});

stopImmediatePropagation()

If you have multiple event handlers for the event, and you want to stop all other event handlers and current event also from further executing, you should use stopImmediatePropagation().

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<html>
    <body>
    <div>
      <a href="#">Blah</a>
    </div>
    <script type="text/javascript">
      $("div a").click(function () {
         alert('Hello!');
      });
       
      $("div a").click(function (e) {
         alert('Hi! You can see me');
         e.stopImmediatePropagation();
      });
       
      $("div a").click(function () {
         alert('I will never popup :( ');
      });
    </script>
    </body>
</html>

So next time when you use return false, think twice do you really want return false or e.preventDefault() or other methods explained above will well-suited there.

Leave a comment

 

Welcome to my Blog

Kalpesh MehtaHelping Magento developers in their day-to-day development problems since 2011. Most of the problems and solutions here are my own experiences while working on different projects. Enjoy the blog and don't forget to throw comments and likes/+1's/tweets on posts you like. Thanks for visiting!

Certifications