JavaScript: reset all event handlers to defaults / disable custom handlers

Is it possible to somehow reset all the event handlers / listeners, attached in course of execution of JavaScript code on the page to their default values, i.e. disabling all the custom functionality, as if the page was loaded without JavaScript support?

I know that I can, for example, reset handlers on individual elements with JQuery's $(selector).off(), but when yet another "clever" site overrides right click, Ctrl+A, Ctrl+arrows or anything like that and I just want to turn it all off ? sometimes I'm not very inclined to dig through their code to find out what they've attached it to, and would love a simpler, one-size-fits-all solution. Is there one?

UPDATE: Found similar question Remove All Event Listeners of Specific Type ? where an accepted answer states that it's impossible for a specific event type. Is it still true as of 2016?

Answers 1

  • If you want to unbind a specific event, like a click, try and iterate through all the children of the body element, like this:

    $('body').children().off('click');
    

    Here is a JSFiddle.

    If you need to unbind multiple different events, try creating a function and passing it an array of events to unbind.


Related Articles