RemoveEventListener without knowing the function

Some third-party plugins attach eventListener to the site. How to remove eventListener without knowing the function associated with it.

I call it removeEventListener , but I cannot figure out how to remove it.

For example: getEventListeners(window) shows related events. But, when I try to delete an event using window.removeEventListener("eventname") , it does not work without knowing this function.

Please help, thanks in advance.

+5
source share
2 answers

getEventListeners(window) will return a map of events and registered registered event listeners.

So, for a DOMContentLoaded event, for example, you can have many event listeners. If you know the index of the listener that you want to delete (or only one exists), you can do:

 var eventlistener = getEventListeners(window)["DOMContentLoaded"][index]; window.removeEventListener("DOMContentLoaded", eventlistener.listener, eventlistener.useCapture); 
+8
source

Unfortunately, you cannot do this. You need to have a reference to the event handler function to remove it using removeEventListener .

Your only option, if you cannot get this link, completely replace this Node .

+4
source

Source: https://habr.com/ru/post/1206568/


All Articles