0

I'd like to ask somthing that maybe is wrong, but I not sure.

Is there a way to know when a specific function is executed, in order to run a sample of code? I like to make it like an event.

The problem I have is the thick box. I like to resize the thickbox according to the image that display.

To do so, I need to know when the thick box is executed.

Any idea please ?

KodeFor.Me
  • 13,069
  • 27
  • 98
  • 166
  • Just a note: [thickbox is not maintained any longer](http://jquery.com/demo/thickbox/). – Zeta Mar 17 '12 at 09:36

3 Answers3

2

You could overload the thickbox plugin invocation.

I'm going to assume it is called on a collection with thickbox(), e.g. $('.container img').thickbox().

(function($) {

    var original = $.fn.thickbox;

    $.fn.thickbox = function() {
        // Whatever you need to do here.
        return original.apply(this, arguments);
    }

})(jQuery);

Now, when you call...

$('.container img').thickbox()

...the code will call your new function before handing the control over to the original function.

You can do whatever you want where it says // Whatever you need to do here. :)

alex
  • 479,566
  • 201
  • 878
  • 984
  • unfortunately looks like not working :(. I have try "console.log('Hello');" before the return original.apply(this, artguments); with no response :( – KodeFor.Me Mar 17 '12 at 09:30
  • 1
    @MerianosNikos Remember I made an assumption. How do you call thickbox? – alex Mar 17 '12 at 09:34
  • It's ok I know :) xaxaxa. As you see I already have made up rating ;). I have load the WordPress default Thickbox implementation, that now allowing me to resize it. :( – KodeFor.Me Mar 17 '12 at 09:36
2

I am not a JS developer but you can create a callback function and make it execute when that specific function is executed.

You can find a good explanation here: JavaScript Callback Scope

Community
  • 1
  • 1
Mert Akcakaya
  • 3,109
  • 2
  • 31
  • 42
2

Thickbox use global function, so you could do below: (As @alex suggested.)

(function($) {
    var original = tb_show;
    tb_show = function () {
        $(document).trigger('tb_show');
        return original.apply(this, arguments);
    }
})(jQuery);

Then you could bind the event:

$(document).bind('tb_show', function() {
    //event handler
});
xdazz
  • 158,678
  • 38
  • 247
  • 274