[Javascript] Bubbling and canceling events

Philip Thompson philthathril at gmail.com
Wed Jun 11 08:40:22 CDT 2008


I figured out the problem. You are correct, Peter, about sending the  
event to the function. I thought that might have been a problem  
before, so I modified the *real* function to accept 'event'. For  
example...

[html]
<a href="javascript:void(0);" onclick="printPathReport(event, 1,  
2);">Print</a>

[js]
function printPathReport (e, file_id, req_id)
{
   //... do stuff here
   if (!e) var e = window.event;
   if (e.cancelBubble) e.cancelBubble = true
   if (e.stopPropagation) e.stopPropagation();
}

This actually works. Seems like the key was to send the event. Now the  
parenting element's event does not fire. Yay! Well, now that I think  
about it, I haven't test in IE yet. Woe is me - now I know what to do  
next. =/

Thanks for your advice. This has been driving me nuts for months!!

~Philip


On Jun 10, 2008, at 10:29 PM, Peter Brunone wrote:

> Hi Philip,
>
>    Does this misfire in all browsers, or just in Firefox and family?
>
>    It is my understanding that in order to use the (Mozilla only)  
> event parameter in your function, you have to include it as a  
> parameter in your function definition.  So, to draw from your  
> example below, if you are referring to this event as "e", then you  
> need to specify the parameter as such like this:
>
> function printCard (e)
>
>    No parameter is necessary when you call the function from the  
> event handler, and IE won't see it; only Mozilla will.
>
> HTH,
>
> Peter
>
> ----------------------------------------
>
> From: Philip Thompson philthathril at gmail.com
>
> Hi all.
>
> In the application I'm working on, I keep coming back to this similar
> problem. I'll just show you here...
>
> Johnny Appleseed	 12/3/1981	 Print Card	
>
> Ok, let's say the user clicks on "Print Card". This is a javascript
> function that performs some AJAX-related things in the background (in
> this case, prints a card). After clicking to print, I wish nothing
> more to happen - I wish to cancel the set of events that follow. In
> this case, I want to cancel the call to the javascript function
> goToUrl() as specified in the
>
> element - I do not want to visit
> the view user page. To accomplish this, I attempted the following:
>
> function printCard () {
> if (!e) var e = window.event;
> e.cancelBubble = true;
> if (e.stopPropagation) e.stopPropagation();
>
> //... do stuff here
> }
>
> However, it still redirects me!! Ahhhh! I can't handle it anymore. Is
> there anyway to cancel the redirection/events? I would be greatly
> appreciated if someone could shed some much-needed light.
>
> Thanks in advance,
> ~Philip
>
> Note: this is a somewhat simplified example of the real thing.
>
>
> _______________________________________________
> Javascript mailing list
> Javascript at lists.evolt.org
> http://lists.evolt.org/mailman/listinfo/javascript

"Personally, most of my web applications do not have to factor 13.7  
billion years of space drift in to the calculations, so PHP's rand  
function has been great for me..." ~S. Johnson




More information about the Javascript mailing list