473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

when should I prefer stopPropagation to simply returning false?

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?

Oct 8 '06 #1
3 2296
VK

Jake Barnes wrote:
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();
}
In the latter case you have to provide a separate statement for IE,
because it doesn't have preventDefault() method, it uses instead
returnValue property you have to assign false for the same effect.

Moreover, you latter case doesn't work at all because preventDefault()
/ returnValue are members of the event object, which is not default in
the function context.

Morover some older (but not too old to disregard) versions of non-IE
browsers still implement W3C-only event model, where the event object
is supplied as the first argument of the event handler: not as global
variable [event] like in IE schema.

So to bring it all together, we have to produce an ugly mess like:

function alertDanger(evt) {
if ((evt)&&(evt.preventDefault)) {
evt.preventDefault();
}
else if ((event)&&(returnValue in event)) {
event.returnValue = false;
}
else {
// out of ideas...
}
}

Browser producers did not want to be beat down by developers for that
yet everyone wanted to stay on theirs. So as a compromise they came up
with an universal shortcut: returning false from the event handler will
mean e.preventDefault() / event.returnValue = false / whatever.

There is one legacy exception in this schema: to prevent default action
in window.onerror handler, you have to return true, not false. (That is
because window.onerror collector was made before any full-scaled event
schemas).

Oct 8 '06 #2
VK
I just noticed that your post is called "when should I prefer
stopPropagation to simply returning false?" which is a typo:
stopPropagation / cancelBubble have nothing to do with the subject of
your post (preventDefault vs. return false).

Just in case for other potential readers.

Oct 8 '06 #3
VK

VK wrote:
else if ((event)&&(returnValue in event)) {
else if ((event)&&('returnValue' in event)) {

That must be string with property name, not property itself, my typo,
sorry. Yet I hope no one will ever have to use this a la Browser Wars
agglomeration. Simply return false in the right places and be happy...

Oct 8 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
by: Steven Bethard | last post by:
I have lists containing values that are all either True, False or None, e.g.: etc. For a given list: * If all values are None, the function should return None.
2
by: Jason Burr | last post by:
I keep getting a server error 400 - Bad Request with the code below. xmlDoc is the MSXML.DOMDocument populated with the xml data to submit. That data is well formed and works fine if I urlencode...
14
by: Michael Winter | last post by:
In an attempt to answer another question in this group, I've had to resort to calling the DOM method, Node.removeChild(), using a reference to it (long story...). That is, passing Node.removeChild....
102
by: Skybuck Flying | last post by:
Sometime ago on the comp.lang.c, I saw a teacher's post asking why C compilers produce so many error messages as soon as a closing bracket is missing. The response was simply because the compiler...
6
by: Derrick | last post by:
Hello all; Since I do have working code, this is more for my curiosity only. I'm creating a "Plugin" architecture, following some of the many examples on the 'net. Basically what I have is...
3
by: Marcel van den Hof | last post by:
Dear all, Is there any way to prevent the ASP.NET worker process from recycling the worker process when a thread is being executed on the foreground (IsBackground=false). I'd also like to...
9
by: Thomas Mlynarczyk | last post by:
Hi, It seems to be a generally adopted convention to have a function return FALSE in case of an error. But if a function is supposed to return a boolean anyway, one cannot distinguish anymore...
6
by: hemant.singh | last post by:
Hi all, I am trying to get a way by which I'll know exactly when user goes out of my site by clicking on close button in browser, So that w/e user click close button in browser, I can send a...
0
by: raylopez99 | last post by:
I ran afoul of this Compiler error CS1612 recently, when trying to modify a Point, which I had made have a property. It's pointless to do this (initially it will compile, but you'll run into...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.