Reviewing some JavaScript codes, I find a peculiarity, sometimes it is used preventDefault()
but in others a return false;
, for example:
function with return false;
:
function() {
return false;
}
function with preventDefault();
:
function(e) {
e.preventDefault();
}
A more specific example would be the following:
$('a').click(function () {
return false;
});
//----------------------------
$('a').click(function (e) {
e.preventDefault();
});
Update
Álvaro 's answer has clarified some doubts and has generated others, for example, he mentions:
In jQuery,
return false
it's equivalent to doingpreventDefault
andstopPropagation
Which with your examples is quite understandable, but continuing with my research, I see two more events that work with similar purposes , the first is stopPropagation
and the other is stopImmediatePropagation
. stopPropagation
So far I have understood it, but what about del stopImmediatePropagation
?
The examples that have been answered so far have helped me a lot to understand how it works, however, now it is not clear to me when it is feasible to use stopImmediatePropagation
.
For example, focusing on jQuery, what would be the result of executing the following code?
$('a').click(function (e) {
e.preventDefault();
e.stopPropagation();
e.stopImmediatePropagation();
});
Given the above, I have a few questions:
- What is the difference between use
preventDefault()
andreturn false;
? ( Doubt resolved ) - When to use one or the other? ( Doubt resolved )
- Is it preferable to lean towards one of the two mentioned? ( Doubt resolved )
- What is it for
stopImmediatePropagation
and what advantages would I have when using it? - What difference is there when using
stopPropagation
againststopImmediatePropagation
or using them together?
Some differences:
preventDefault
, as its name suggests, prevents the default action associated with that event; while itreturn false
returns the value false (something that could sometimes prevent the default action, but that is not its main objective).preventDefault
it's an event method, that is, it's a function inside an object and it doesn't work without its event (you can't just dopreventDefault()
it, it has to beevento.preventDefault()
); While itreturn
is a statement that returns a value and can be called at any time.preventDefault
can be calledpreventDefault
at any time, whereasreturn false
it can only be done at the end. This may seem trivial, but it can have different results if there is some JS error in the middle of the event handler.Although they may work similarly in some cases, this is not always the case . For example: in pure JavaScript, if you have a link with
a
and dopreventDefault()
, the page will not redirect because the default action is prevented. While if you doreturn false
yes, the redirection is performed.But this is not always the case: if you use
return false
inlineonclick
instead of using , then the default event will be avoided (for example with forms or links) .addEventListener
That's with pure JavaScript, inside jQuery oddly enough they work a bit differently. In jQuery,
return false
it's equivalent to doingpreventDefault
andstopPropagation
. In other words, not only would the default action be avoided, but the propagation of the event to the ancestors of the element would also be avoided.An example of this:
On StackOverflow in English, there is a question that goes into this topic with jQuery in more depth.
Like everything: it will depend on the context and what you want to do. Especially if you're working with jQuery, where
return false
it does a lot more than just prevent the default action.As I put above, it will depend on the context and if you are using pure JavaScript or some library like jQuery. I personally like to use
preventDefault
, but it's my own choice.You should also think about other factors like what I put in the difference in positioning: if an error occurs in the event handler,
return false
it will never run. I know that it is not really something so serious (you have an error, that is more serious than not reaching thereturn
), but a detail to take into account.When multiple event handlers are attached to an element, they are executed in the order in which they were attached.
stopImmediatePropagation
causes subsequent handler calls to stop.It is important to note that it not only prevents the rest of the event handlers associated with that element from executing, but it also calls
stopPropagation
, pot which prevents the event handlers of the ancestors from being called.stopPropagation
stops propagation of events to the element's ancestors (stops the "bubble" effect). For example, in the script above, when donereturn false
in jQuery (and thus executedstopPropagation
), only the del event handler was executed,a
but not the del event handlerdiv
.But if the element has more event handlers,
stopPropagation
it won't stop them, it will just stop propagation to the ancestors. WithstopImmediatePropagation
, it does stop propagation to controllers associated with that element.Also, as I also said in the previous point,
stopImmediatePropagation
callstopPropagation
. That is why it makes no sense to use them together: If you are going to use both, just usestopImmediatePropagation
.Let's give an example to illustrate it: I have an element and I assign 3 click event handlers to it (A, B and C), where B has a
stopImmediatePropagation
. Controllers are executed in the order they were attached, so when you click, A will be executed, then B... and C will never be executed.That example is a bit simple so you don't see the usefulness of
stopImmediatePropagation
, but imagine that you have a form in which you are going to do validation, if something fails you can dostopImmediatePropagation
so that if there is any other event associated with the form, it will not be run.answering the questions,
1.- Just
preventDefault()
as it indicates its name, it cancels the default action or response. For example, pressing a link prevents it from going to the path specified in the href, or prevents submitting a form when clicking the submit button, etc.return false
stops code execution and exits the function block.2.- If you have a function where your parameter is an array, and there are several actions on that parameter, but if it comes empty or undefined, you probably want to stop the process by returning false. In the event that you want to submit a form, for example by ajax, it is recommended that when submitting, stop the propagation of the submit action of that form with
preventDefault()
, but continue with the following line of code, which will take care of to submit the form by ajax.3.- You should lean towards what you really need, but in general, to cancel input actions or DOM elements with jQuery, it is not recommended to use
return false
Calling
stopPropagation
within a handler declared on an element prevents the event from escalating up the DOM hierarchies, but it does not prevent handlers declared on that element from executing.The behavior of stopImmediatePropagation , on the other hand, not only implies (implicitly)
stopPropagation
but also, given the existence of several handlers, and that they are executed in sequence, the call tostopImmediatePropagation
in one of the handlers interrupts the sequence, leaving the remaining handlers unexecuted .The order of the sequence in which the handlers are executed is a queue that respects the order in which they were declared.