Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to list all custom events created?

I know it is possible to add an event listener to a custom event that I have created in Javascript, like this:

window.addEventListener("MyCustomEvent", myFunction, false);

But...Is it possible to list All custom events that exist at any point?

The above line adds an event listener regardless of whether the event exists or not, so I cannot indicate if the event exists or not.

like image 280
Oli C Avatar asked Jul 31 '14 15:07

Oli C


1 Answers

This is generally a bad idea, but if you really have the need for this, you could override the addEventListener function like this to keep track of the events added:

var events = {};
var original = window.addEventListener;

window.addEventListener = function(type, listener, useCapture) {
    events[type] = true;
    return original(type, listener, useCapture);
};

function hasEventBeenAdded(type) {
    return type in events;
}

Keep in mind that this will only work for code that adds event listeners after this piece of code is included.

like image 168
Overv Avatar answered Sep 28 '22 23:09

Overv