Return false will perform three task when called
1 event.preventDefault();
2 event.stopPropagation();
3 Stops callback execution and returns immediately when called.
The preventDefault() method cancels the event if it is cancellable, meaning it prevents the default action that belongs to the event to occur.
The benefit to using event.preventDefault() is that you can add this as the first line in the handler, thereby guaranteeing that the anchor's default behavior will not fire, regardless if the last line of the function is not reached (eg. runtime error) which is not the case .lets assume of return false If your click handler uses return false to prevent browser navigation, it opens the possibility that the interpreter will not reach the return statement and the browser will proceed to execute the anchor tag's default behavior.
return false example.
$('a').click(function (e) {
// custom handling here
// oops...runtime error..
return false;
});
preventdefault() example.
$('a').click(function (e) {
e.preventDefault();
// custom handling here
// .runtime error, but the user isn't navigated away.
});
0 Comment(s)