Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery firefox stopPropagation()

I'm binding two event handlers to an input field on 'keydown'. If the enter key has been pressed, the first event handler needs to stop the propagation of the event so that it doesn't hit the second eventhandler. I'm doing it like so:

if (jQuery.browser.msie) {
                    event.cancelBubble = true;
                } else {
                    event.stopPropagation();
                }

now this alone doesn't stop the event propagation either in IE or Firefox. It hits the first event handler, and then hits the second event handler as well. However, in the second event handler, I can actually check if (e.cancelBubble) in case of IE. Is there a way to check the same with Firefox?

like image 268
Riz Avatar asked Jan 08 '10 18:01

Riz


People also ask

What is stopPropagation ()?

stopPropagation() The stopPropagation() method of the Event interface prevents further propagation of the current event in the capturing and bubbling phases. It does not, however, prevent any default behaviors from occurring; for instance, clicks on links are still processed.

What's the difference between event preventDefault () and event stopPropagation ()?

The event. preventDefault() will not allow the user to leave the page and open the URL. The event. stopPropagation() method stops the propagation of an event from occurring in the bubbling or capturing phase.

How do I stop Javascript from bubbling?

To stop an event from further propagation in the capturing and bubbling phases, you call the Event. stopPropation() method in the event handler. Note that the event. stopPropagation() method doesn't stop any default behaviors of the element e.g., link click, checkbox checked.

What is the difference between event stopPropagation and event stopImmediatePropagation?

stopPropagation allows other event handlers on the same element to be executed, while stopImmediatePropagation prevents this. stopPropagation and stopImmediatePropagation prevents event handlers later in the capturing and bubbling phases from being executed.


1 Answers

Just remove your test for IE and use this:

event.stopImmediatePropagation();

Which will keep other events from firing in both browsers.

event.stopPropagation() will keep events from bubbling, but won't keep other event handlers for the same object from firing.

To answer your other question, if you just used event.stopPropagation() you could check event.isPropagationStopped() in the second handler.

Suggestion: as a general rule jQuery fully abstracts all browsers behavior to provide a single interface to the functionality. If you find yourself running if(jQuery.browser.msie) before running a jQuery function, there is probably a better way to run it that will work cross browser. And, when you do need to test, you should use jQuery.support to test for functionality not specific browser sniffing.

like image 65
Doug Neiner Avatar answered Sep 19 '22 22:09

Doug Neiner