J
Jake Barnes
On this page I'm given the impression that stopPropagation is a lot
like returning false:
http://www.brainjar.com/dhtml/events/default3.asp
"preventDefault() Can be used to cancel the event, if it is
cancelable. This prevents the browser from performing any default
action for the event, such as loading a URL when a hypertext link is
clicked. Note that the event will continue propagating along the normal
event flow.preventDefault() Can be used to cancel the event, if it
is cancelable. This prevents the browser from performing any default
action for the event, such as loading a URL when a hypertext link is
clicked. Note that the event will continue propagating along the normal
event flow."
But often, if I want to stop a hyperlink from working normally, I can
simply return false from whatever action I've attached to that
hyperlink. Suppose I have this link:
<a id="link1" href="index.htm">link text</a>
Suppose I attach the function "alertDanger" to this link:
function alertDanger() {
alert("Danger!");
return false;
}
Because I return false, the default behavior doesn't occur. Would there
ever be a benefit to doing this instead:
function alertDanger() {
alert("Danger!");
preventDefault();
}
Perhaps preventDefault is only for those occassions when the code can
not or should not return?
like returning false:
http://www.brainjar.com/dhtml/events/default3.asp
"preventDefault() Can be used to cancel the event, if it is
cancelable. This prevents the browser from performing any default
action for the event, such as loading a URL when a hypertext link is
clicked. Note that the event will continue propagating along the normal
event flow.preventDefault() Can be used to cancel the event, if it
is cancelable. This prevents the browser from performing any default
action for the event, such as loading a URL when a hypertext link is
clicked. Note that the event will continue propagating along the normal
event flow."
But often, if I want to stop a hyperlink from working normally, I can
simply return false from whatever action I've attached to that
hyperlink. Suppose I have this link:
<a id="link1" href="index.htm">link text</a>
Suppose I attach the function "alertDanger" to this link:
function alertDanger() {
alert("Danger!");
return false;
}
Because I return false, the default behavior doesn't occur. Would there
ever be a benefit to doing this instead:
function alertDanger() {
alert("Danger!");
preventDefault();
}
Perhaps preventDefault is only for those occassions when the code can
not or should not return?