Tuesday, July 13, 2010

return false vs preventDefault

“Steal” it from a comment of this article.


If there is javacsript error in page, using return false browser will perform default action. That can be avoid using preventDefault(). Example:


$('a').click( function(e){
$(this).css("background", "green");
err(); // err is not defined, so javascript will throw error, and browser will ignore rest of the code (return false). Browser will perform default action.
return false;
});

$('a').click( function(e){
e.preventDefault();
$(this).css("background", "green");
err(); // err is not defined, so javascript will throw error, and browser will ignore rest of the code. We already called preventDefault() so browser will not perform default action.
});

Xem đầy đủ bài viết tại http://feedproxy.google.com/~r/ducban/~3/QCSdq25YIeY/805993987

No comments:

Post a Comment

Popular Posts