3

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?

Community
  • 1
  • 1
Kanpeki
  • 63
  • 1
  • 4
  • i think thats hard, you can use "*" as selector... but then you still have all the other events, which are not attached to an object... or is window included in "*"? keep trying! – fucethebads Apr 07 '16 at 05:35

3 Answers3

2

Maybe you want try this?

$("*").unbind();

http://www.w3schools.com/jquery/event_unbind.asp

Lin Yuan
  • 528
  • 2
  • 12
1

Something like this should be possible (but I haven't tried):

Make a GreaseMonkey script or browser extension that executes at document start (similarly for browser extensions). It should redefine EventTarget.addEventListener to add the event name, target and handler to an array, then invoke the original function. You can then iterate over the array and removeEventListener all of them at will. Alternately, for a more aggressive approach, you could redefine EventTarget.addEventListener to be a no-op.

This does not solve any handlers declared as onXXXXX fields; you would need to seek and destroy those manually.

Community
  • 1
  • 1
Amadan
  • 191,408
  • 23
  • 240
  • 301
0

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.