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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With