Best way to search for events?

I recently found myself in a situation where I needed to remove a function related to a window resize event using the WordPress media editor (media-upload.js), because it prevented the proper use of Thickbox. The event is attached as follows:

a(window).resize(function(){tb_position()}) 

It took me a while, but I finally found out that I can do this as follows:

 jQuery.each( jQuery(window).data('events')['resize'], function(i, event) { var thisEvent = event.toString().replace(/\n/g, '').replace(/\t/g, '').split(' ').join(''); var expectedEvent = 'function(){tb_position()}'; if (thisEvent == expectedEvent) delete jQuery(window).data('events')['resize'][i]; }) 

Here I look at events, removing spaces, tabs and newlines from them, comparing them with what I'm looking for, and when I find this, I throw it out of the damned gateway. It happens that in the attached function there may not be spaces, tabs or new lines, but this method also works with more complex functions, as far as I can tell.

Is there an easier and / or more elegant way to do this? Is this a recipe for disaster along the way?

+4
source share
2 answers

When you register a handler for an event, you can use the qualifier:

 $('#something').bind('click.removeMeSomeday', function() { ... }); 

Then, when you need to remove it, you can do it without disturbing other click handlers.

Now it comes to my mind that you will not be able to influence how Wordpress binds its event handler.

+1
source
+1
source

Source: https://habr.com/ru/post/1302631/


All Articles