removeEventListener without knowing the function

前端 未结 2 1429
北荒
北荒 2020-12-03 10:20

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.

相关标签:
2条回答
  • 2020-12-03 10:55

    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);
    
    0 讨论(0)
  • 2020-12-03 11:17

    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.

    0 讨论(0)
提交回复
热议问题