0

I am not sure if this is possible. I would like to pass in a function name as parameter like this

loadContent("http://test.com/", specialFunction);

specialFucntion is just a string :

function loadContent(href, functionIWant){
    $.ajax({
        type: "GET",
        url: href,
        dataType: "json",
        success: function(res, textStatus, xhr) {
            helper();
            functionIWant + "()"; //So this would be treated as function
                                  //and it actually calls specialFunction()
                                  //is it possible for this?
        }
    });
}

How do I achieve this?

ADD-ON

Let's say, I would pass in an array of function names, how do I call it?

for(var i = 0; i < functionIWant.length; i++){
   functionIWant[i]; // how? appeciate a lot :)
}
Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247
Loonb
  • 1,068
  • 5
  • 18
  • 30

4 Answers4

3

You can just do it with functionIWant()

Example using your provided snippet:

function loadContent(href, functionIWant)
{
    $.ajax({
        type: "GET",
        url: href,
        dataType: "json",
        success: function(res, textStatus, xhr) {
            helper();
            functionIWant();
        }
    });
}

For your addendum

Assuming the following three functions you want to call

function foo() {alert("Foo")}
function bar() {alert("bar")}
function baz() {alert("baz")}

The best I can recommend if you're passed an array of function names as strings is to follow the advice here. Basically, you could just do it like this:

// Assuming FunctionIWant is an array of strings that represent function names
FunctionIWant = ["foo","bar","baz"];

...

for(var i=0;i<FunctionIWant.length;i++)
    window[FunctionIWant[i]]();

If, however, FunctionIWant is an array of actual functions, for example, you can simply iterate over them and call them individually like so:

FunctionIWant = [foo,bar,baz] // note, these are not strings

for(var i=0;i<FunctionIWant.length;i++)
    FunctionIWant[i]();

In most cases, you'll be better off assigning the function rather than as a string

Community
  • 1
  • 1
chops
  • 2,572
  • 1
  • 16
  • 25
  • chops, question updated. thanks for this straightforward answer :) – Loonb Jan 20 '13 at 04:23
  • Glad that helped a little. I've updated my answer to accommodate your addendum. – chops Jan 20 '13 at 04:51
  • @chops - Why not just pass an array of actual functions? e.g. `loadContent('path/to...', [function1, function2])`. Then you can just call the functions directly. – Joseph Silber Jan 20 '13 at 04:56
  • No doubt. I was assuming he was passing the function names as strings (for the addendum, anyway) since he used the term "an array of function names", rather than "an array of functions". I'll modify my answer to extend this. – chops Jan 20 '13 at 05:27
2

If you want to try to call the function represented by a parameter, simply call it as if that were the name of the function:

function loadContent(href, callback)
{
    $.ajax({
        type: "GET",
        url: href,
        dataType: "json",
        success: function(res, textStatus, xhr) {
            helper();
            callback();
        }
    });
}
Platinum Azure
  • 45,269
  • 12
  • 110
  • 134
1

I am guessing that functionIWant is a string, not a reference to a function [You should make that clear when asking the question]

If that is the case, you want

window[functionIWant]();

That will work if the function is in the global scope. It would be better that you pass in the reference to the function or if you namespaced your functions.

var myFuncs = {
    foo : function(){ alert("asdf"); },
    bar : function(){ alert("qwerty"); }
};

var functionIWant = "foo";
myFuncs[functionIWant]();
epascarello
  • 204,599
  • 20
  • 195
  • 236
  • thanks for pointing out i didn't make it clear it is a reference or string. :) i haven't try your solution. i'll try later :) – Loonb Jan 20 '13 at 04:42
0

for example you have two functions,

var function1 = function( value ) {
  console.log( "foo: " + value );
};

var function2 = function( value ){
  console.log( "bar: " + value );
};

and in functionIWant array you have name of function,

function loadContent(href, functionIWant)
{
   $.ajax({

    type: "GET",
    url: href,
    dataType: "json",
    success: function(res, textStatus, xhr) {
    helper();

    var callbacks = $.Callbacks();

    for(var i = 0; i < functionIWant.length; i++){
     callbacks.add( functionIWant[i] ); //on i=0 add function1
     callbacks.fire( "hello" );          // will fire function1
    }



      }
   });
  }
sourcecode
  • 1,802
  • 2
  • 15
  • 17