9

I have this jquery function

    function example(file, targetwidget, callback){

    $(targetwidget).load(file, {limit: 25}, function(){
        $("#widget_accordion").accordion({fillSpace: true});
    });
}

it's working ok when I do:

 example('http://example.com/', "#divid", callback);

but I want to send the callback function inside the (callback) variable i.e: instead of butting the $("#widget_accordion").accordion({fillSpace: true}); inside the callback function I want to send it:

 example('http://example.com/', "#divid", '$("#widget_accordion").accordion({fillSpace: true});');

and then the function must be something like:

function example(file, targetwidget, callback){

$(targetwidget).load(file, {limit: 25}, function(){
    callback;
});

but that's not working

Thanks in advance for your help

Bassel Safadi
  • 487
  • 1
  • 3
  • 11

5 Answers5

19

To pass the callback around, the variable needs to be of type function. Any of these should work:

function example(file, targetwidget, callback) {
  $(targetwidget).load(file, {limit:25}, callback);
}

// Call it by providing the function parameter via inline anonymous function:
example('http://example.com/', "#divid", function() {
  $("#widget_accordion").accordion({fillSpace: true});
});

// Or, declare a function variable and pass that in:
var widgetCallback = function() {
  $("#widget_accordion").accordion({fillSpace: true});
};

example('http://example.com/', "#divid", widgetCallback);

// Or, declare the function normally and pass that in:
function widgetCallback() {
  $("#widget_accordion").accordion({fillSpace: true});
}

example('http://example.com/', "#divid", widgetCallback);
Dave Ward
  • 59,815
  • 13
  • 117
  • 134
  • to add onto this great answer, you may want to add validation to this as well, I suggest using jquery's $.isFunction() (http://api.jquery.com/jQuery.isFunction/) – Eman Jul 09 '12 at 20:07
2

you need to invoke the function inside the callback

function example(file, targetwidget, callback){

$(targetwidget).load(file, {limit: 25}, function(){
    callback();
});
redsquare
  • 78,161
  • 20
  • 151
  • 159
1

This should work:

$(targetwidget).load(file, {limit: 25}, callback);
peirix
  • 36,512
  • 23
  • 96
  • 126
1

well.. You do this way:

function example(file, targetwidget, callback){

$(targetwidget).load(file, {limit: 25}, function(){
    if(typeof(callback)==='function'){
     callback.call(this, 'other parameters');
    }
});

The paramenters of callback will help you to send data back to callback function. ATTENTION, don't forget the this keyword!

Ionuț Staicu
  • 21,360
  • 11
  • 51
  • 58
1

Since you are passing a text string you would need to eval it:

function example(file, targetwidget, callback){

    $(targetwidget).load(file, {limit: 25}, function(){
        eval(callback);
    });

Edit:

if you want to use call() you would have to do it like this:

function callback() {
    $("#widget_accordion").accordion({fillSpace: true});
}

function example(file, targetwidget, callback){

    $(targetwidget).load(file, {limit: 25}, function(){
        if(typeof(callback)==='function'){
            callback.call(this);
        }
    });

example('http://example.com/', "#divid", callback);

In order for call to work, you would have to pass a function and not a string. But by wrapping your jQuery expression inside of a function, it would be run when the function is called. Calling a function can be done with the .call() method.

googletorp
  • 33,075
  • 15
  • 67
  • 82
  • 1
    answered 3 mins ago googletorp 666●10 -> and we all know eval is evil! – peirix Jul 20 '09 at 12:27
  • Yeah, read my comment http://stackoverflow.com/questions/1153241/send-a-jquery-callback-function-inside-a-variable/1153272#1153272 . Just use call() instead of eval() – Ionuț Staicu Jul 20 '09 at 12:37