views:

93

answers:

2

I recently found myself in the situation that I needed to remove a function bound to the resize event of the window by WordPress' media manager (media-upload.js), because it was interfering with the proper use of Thickbox. The event is attached like this:

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

It took me a while, but I finally found out I could do it in this way:

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 cycle through the events, removing spaces, tabs and new lines from them and compare them to what I'm looking for, and when I find it I throw it out of the goddamn airlock. It happens in this case that the attached function perhaps doesn't have spaces, tabs or new lines, but this way also works with more complicated functions as far as I can tell.

Is there an easier and/or more elegant way of doing this? Is this a recipe for disaster down the road?

+1  A: 

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

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

Then when you need to remove it you can do so without bothering other handlers for "click".

Now, it occurs to me that you may not be able to affect the way that Wordpress binds its event handler.

Pointy
Yes, I actually know that, and use it in my own code. However, as you also mention, there's not a lot I can do about how WordPress is binding its event handler.
Heilemann
+1  A: 

Another way around might be to use WordPress' system for queueing/unqueueing or registering/deregistering scripts. Unregister media-upload.js, and then queue your own version of it.

Prem

Premasagar