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.
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.
Share Improve this question edited Jul 31, 2014 at 15:53 Mike Cluck 32.5k13 gold badges83 silver badges94 bronze badges asked Jul 31, 2014 at 15:49 Oli COli C 1,17814 silver badges38 bronze badges 2- 5 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. – adeneo Commented Jul 31, 2014 at 16:02
- Thanks adeneo. I Am actually trying to debug via the console, but the above mand 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..! – Oli C Commented Aug 1, 2014 at 11:34
1 Answer
Reset to default 13This 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.