0

I'm running this snippet of jQuery to remove all event listeners from my ui-router based angular app, but it doesn't seem to do anything? Is it an illegal operation?

$rootScope
    .$on('$stateChangeStart', 
        function(event, toState, toParams, fromState, fromParams){ 
            console.log("Going to remove event listeners"); // this fires
            $(document).off();
    });
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
rex
  • 3,133
  • 6
  • 35
  • 62
  • I also tried running it in the chrome dev console, with no errors. I still see all the events under Elements tab >> "Event Listeners" – rex Aug 03 '16 at 17:57
  • Does this answer your question? [how to unbind all event using jquery](https://stackoverflow.com/questions/3569393/how-to-unbind-all-event-using-jquery) – Brian Tompsett - 汤莱恩 May 02 '20 at 14:34

1 Answers1

0

It would seem that you need to select the elements specifically in order to remove event handlers from them. You can remove all events from an object with .unbind(), but I don't believe you can do this with the whole page. See this question.

Community
  • 1
  • 1
HighHopes
  • 2,014
  • 3
  • 23
  • 33
  • the events are registered on the document though based on the screenshot? http://i.stack.imgur.com/Rea95.png – rex Aug 03 '16 at 18:07
  • Anyway ive tried doing this too and it doesnt seem to work? $("html").off() or $("body").off() – rex Aug 03 '16 at 18:10
  • Yes, I see that. Well, someone with greater knowledge than me is going to have to take this one. :D – HighHopes Aug 03 '16 at 18:11
  • Just did - no luck :/ – rex Aug 03 '16 at 18:13
  • Are you adding the event handlers to the document when you load the page? I mean, are you doing something like this, `$(document).on('click', function(){})`? – HighHopes Aug 03 '16 at 18:15
  • The events are being created by some third party lib and are not being removed properly. I am trying to remove these without messing with the library code. – rex Aug 03 '16 at 18:26