Is it possible to list all custom events created?
Asked Answered
T

1

17

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.

Tumulus answered 31/7, 2014 at 15:49 Comment(2)
No, it's not possible. The console in the browser should be able to list them, Chrome has getEventListeners etc, but it's only for use in the console, in your script there's no way to list the event listeners added.Winterfeed
Thanks adeneo. I Am actually trying to debug via the console, but the above command does not seem to work whilst in debug mode...Let me explain further- I am having to use JQuery event triggers to create my custom events, as creating them via Javascript is not supported in the Android native browser... I would rather not use JQuery CustomEvents, so I have been trying to work out how JQuery itself creates the events, but introspection into the JQuery code can be kind of confusing..!Tumulus
T
13

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.

Treasure answered 31/7, 2014 at 16:58 Comment(1)
Hi Overv, thanks for your response, though maybe I wasn't as clear as I should have been- My comment above explains better what I require- I am debugging to find out how JQuery creates custom events, so I can take that code and implement it in Javascript.Tumulus

© 2022 - 2024 — McMap. All rights reserved.