Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In a Javascript event, how to determine stopPropagation() has been called?

If e.preventDefault() is called, can see reflected in e.defaultPrevented method.

Is there a similar property for e.stopPropagation()? If not, how to determine?

like image 981
cc young Avatar asked Aug 31 '11 15:08

cc young


2 Answers

I haven't looked through jQuery to see their method, but it seems you could override the stopPropagation method on the base Event object, set a flag, and then call the overridden method.

Something like:

var overriddenStop =  Event.prototype.stopPropagation;
Event.prototype.stopPropagation = function(){
    this.isPropagationStopped = true;
    overriddenStop.apply(this, arguments);
}
like image 123
Josiah Ruddell Avatar answered Sep 23 '22 00:09

Josiah Ruddell


No, there isn't.

I can tell, because jQuery's isPropagationStopped() method doesn't use any browser API internally, but instead implements this feature manually. (If there were such a feature in the browsers - built-in - jQuery would use it instead of doing it manually.)

So, in jQuery, a new event object will get this property (inherited):

isPropagationStopped: returnFalse

and then, when you invoke stopPropagation() on that event object, jQuery will manually alter this property:

stopPropagation: function() {
    this.isPropagationStopped = returnTrue;
    ...
}

returnFalse and returnTrue are functions which return false and true respectively.

like image 25
Šime Vidas Avatar answered Sep 25 '22 00:09

Šime Vidas