473,671 Members | 2,568 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.preventDef ault() 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 "alertDange r" 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 2307
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.pr eventDefault)) {
evt.preventDefa ult();
}
else if ((event)&&(retu rnValue in event)) {
event.returnVal ue = 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.preventDefaul t() / event.returnVal ue = 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)&&(retu rnValue in event)) {
else if ((event)&&('ret urnValue' 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
3369
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
3805
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 it and send it via a GET instead of POST. It also prints out nice if I simply send it out so I know thats not the issue. No matter what I put in there or if I add the data from the document into a querystring nothing works. I am assuming that it...
14
9579
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. In Opera (7.23/Win), the call appears to do nothing - the node remains - but no errors are shown. In Netscape (7.0/Win), an exception results. On IE (6.0/Win), the node is removed. Strangly, if I pass another function reference, say...
102
5649
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 can't tell where a bracket is missing.... a few weeks have past, I requested a feature for the delphi ide/editor "automatic identation of code in begin/end statements etc" and today when I woke up I suddenly released a very simple solution for this...
6
1982
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 this: - a DLL which contains the interface that every plugin must implement (IPlugin). This DLL also contains a class with the ability to search for DLLs in the calling applications working directory that contain classes implementing IPlugin (I...
3
2536
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 know if there a way for a foreground thread to detect if the worker process is being recycled? It seems that when a worker process recycles (under IIS 5.0) a thread
9
5201
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 between the "normal" FALSE and the "error" FALSE. So why not using NULL instead to indicate an error? Are there drawbacks I am not aware of? Greetings, Thomas
6
3934
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 signal to server. This seems to be achievable with body unload events, but it is little too much, as even if user navigate within my site, this event will be generated, this can be avoided by handling onclick of each link, so that I'll know exactly...
0
2186
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 problems later). Apparently Point is a struct, a value type, and it does not behave like a classic structure (in my mind's eye, and see below). Traditionally I think of a classic structure as simply an object where every member is public. But with...
0
8402
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8927
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8825
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8605
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8676
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6237
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4227
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2062
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1816
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.