Agha Usman

Lives in Karachi (Pakistan) and work for Ciber Strategies

Event Bubbling in Javascript

Today, while creating some javascript popup I had a condition in which I place onclick function on body tag. I had an other button which also implement onclick. So the code looks like as follows

   1: <body onclick="disappearPopup()">
   2: <a href="#" onclick="ShowPopup()">Show me</a>
   3: </body>

So, what happen is whenever link is click the body clicked even called automatically. Practically speaking, we need to cancel the body onclick even each time we click the link click.

And for that I write the following code on click of the link

   1: if (window.event) {
   2:     window.event.cancelBubble = true;
   3: }
   4: else {
   5:     e.stopPropagation();
   6: }

And it works ……..

Posted: Jan 30 2009, 03:11 PM by aghausman12 | with 1 comment(s) |
Filed under:

Comments

tsantos said:

That's really useful. Thank's a lot for sharing.

# February 5, 2009 4:59 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)