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?

1
  • 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! Commented Apr 7, 2016 at 5:35

3 Answers 3

2

Maybe you want try this?

$("*").unbind();

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

Sign up to request clarification or add additional context in comments.

2 Comments

Only works on handlers attached by jQuery. If someone uses .addEventListener directly, you're out of luck.
Yeah, and, unfortunately, it doesn't seem to work on onXXX handlers either.
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.

Comments

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.

1 Comment

Check out Lin Yuan's answer for a better solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.