I know you can see the event listeners in the Chrome Inspector but i'm doing some debugging work and there are so many event listeners lying around I'd like to disable some without editing the code
Is there a way to disable an event listener quickly from the Webkit inspector?
Perhaps have a look and type some code into the console to removeEventListener
the listener? How would I do this? For instance how would i remove the 'click' listener above
Using the removeEventListener() method The JavaScript built-in function removeEventListener() removes an event handler from an element for a connected event. For instance, you can use removeEventListener() to get rid of a click event listener if a button is disabled after one click.
removeChild(b); b = null; // A reference to 'b' no longer exists // Therefore the element and any event listeners attached to it are removed. However; if there are references that still point to said element, the element and its event listeners are retained in memory.
Go to the objects tab, view the Document object (don't click on edit) and scroll down to Event Handlers. Select the one to delete and press delete.
You can use getEventListeners(element).click[index].listener
to get a reference to a listener (in a WebKit console).
So, to remove the first listener, you could do:
document.removeEventListener('click', getEventListeners(document).click[0].listener)
Similarly, to remove all listeners, you could use this function:
function removeEventListeners(element, listenerMap) {
Object.keys(listenerMap).forEach(function (name) {
var listeners = listenerMap[name];
listeners.forEach(function (object) {
element.removeEventListener(name, object.listener);
});
});
}
removeEventListeners(document, getEventListeners(document))
Sorry, you are out of luck (at least for the time being.) removeEventListener
requires the exact listener Function object as an argument, and DevTools do not let you get a grip of the listener function in any way.
If you definitely need this feature, please file a bug at http://new.crbug.com (against Chrome) or http://bugs.webkit.org (against WebKit, the preferred way).
You can remove an event listener in the javascript console.
First find the element to which this event listener is attached. Let's call it e. Then you execute:
e.onclick=null
.
For example, many event listeners are attached to "body", then the above code becomes:
document.body.onclick=null
.
After that the event listener is removed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With