12

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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
cc young
  • 18,939
  • 31
  • 90
  • 148

3 Answers3

10

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);
}
eebbesen
  • 5,070
  • 8
  • 48
  • 70
Josiah Ruddell
  • 29,697
  • 8
  • 65
  • 67
  • 4
    I'm afraid this is a [bad idea](http://perfectionkills.com/whats-wrong-with-extending-the-dom/) – Šime Vidas Aug 31 '11 at 16:11
  • you're probably right. very sneaky way of implementing so it's invisible downstream. this might work! need to attack in the morning when my brain works. – cc young Aug 31 '11 at 16:16
  • 1
    @Šime - I agree. I suppose this felt like less of an "extension" since it is not adding new methods to the Event class, albiet instance variables. So would you agree that the best method here would be to make an event object wrapper? – Josiah Ruddell Aug 31 '11 at 16:16
  • @Josiah Well, not make one personally `:)` I don't want to waste my time with such low-level things. There are enough event libraries out there... – Šime Vidas Aug 31 '11 at 16:23
  • 1
    although Extending the DOM is generally a bad idea, @Josiah's solution does not universally extend the DOM's Event, but rather let's me extend particular Event instances in a well defined fashion. the big win is that it lets users keep their existent trigger scripts unchanged. it appears to have no nasty side effects. – cc young Sep 01 '11 at 08:54
  • 4
    This helped me to debug where in the code an event was stopped bubbling. Turn out to be bootstrap :) Thanks! – PSWai Dec 06 '16 at 08:24
5

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.

Šime Vidas
  • 182,163
  • 62
  • 281
  • 385
0

In IE, you can check the property e.cancelBubble to check for native stops. Other browsers do not have this functionality.

However, you could just keep track of it yourself. Because of the nature of it, you wouldn't be able to check it anywhere, except in the same handler that stops the propagation itself (the event won't bubble up, more handlers won't get called), so I can't imagine a situation where it would be necessary. What are you trying to do?

Edit: I thought you were using jQuery at first. For those who are: You can use the e.isPropagationStopped() method, which will check if propagation has been stopped from jQuery. If propogation is stopped natively, this won't be able to tell.

Owen
  • 630
  • 6
  • 21
  • see http://stackoverflow.com/questions/7257581/in-javascript-how-to-determine-and-call-inherited-listeners for context. essentially `change` is not being called and need to initiate it myself – cc young Sep 01 '11 at 05:46
  • if you look at the link, by using `dispatchEvent()` - you're right - this issue is no longer in play – cc young Sep 01 '11 at 08:57
  • I see. Events still bubble correctly when you trigger them manually, so they should obey `.stopPropagation()` already. – Owen Sep 01 '11 at 22:01