Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if event propagation was stopped?

I am writing an event dispatcher in JavaScript and I've decided to base on standard js CustomEvent class. I can't find out how to detect if event propagation was stopped in the listener (via e.stopPropagation()). Should I rather write an Event object implementation myself?

like image 556
Logarytm Całkowy Avatar asked Mar 01 '14 09:03

Logarytm Całkowy


Video Answer


1 Answers

You have two possible solutions:

The first solution

The Event.cancelBubble property is a historical alias to Event.stopPropagation(). But it has the difference to the function Event.stopPropagation() because Event.cancelBubble we could read like follows:

var isPropagationStopped = event.cancelBubble; // true if it was stopped, or false if not

The next solution

You could override the stopPropagation method on the base Event object and set your own flag in the overridden method like follows:

var isPropagationStopped = false;

var stopPropagationFuncTemp =  Event.prototype.stopPropagation;
Event.prototype.stopPropagation = function()
{
    isPropagationStopped = true;
    stopPropagationFuncTemp.apply(this, arguments);
};

Now if isPropagationStopped == true then it is stopped.

like image 109
Bharata Avatar answered Sep 28 '22 06:09

Bharata