Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

removeEventListener without knowing the function

Some of the third party plugin will attach the eventListener into the site. How to I remove the eventListener without knowing the function that attached.

I refer this removeEventListener but I can't get any clue to remove this.

Eg: getEventListeners(window) shows the events attached. But, when I try to remove the event using window.removeEventListener("eventname") is not working without knowing that function.

Please help, Thanks in advance.

like image 400
Dineshkani Avatar asked Nov 10 '14 14:11

Dineshkani


Video Answer


2 Answers

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

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

var eventlistener = getEventListeners(window)["DOMContentLoaded"][index];
window.removeEventListener("DOMContentLoaded", 
                           eventlistener.listener,
                           eventlistener.useCapture);
like image 86
manji Avatar answered Oct 17 '22 03:10

manji


Unfortunately, you cannot do that. You need to have a reference to the event handler function in order to remove it by removeEventListener.

Your only option if you cannot get that reference would be by entirely replacing that Node.

like image 44
jAndy Avatar answered Oct 17 '22 02:10

jAndy