If you pass the function name as a string, you can try the following:
window[functionName]();
But this suggests that the function is in a global area. Another, much better way to do this is to simply pass this function:
function onSuccess() { alert('Whoopee!'); } function doStuff(callback) { callback(); } doStuff(onSuccess);
Edit
If you need to pass variables to functions, you can simply pass them along with the function. Here is what I mean:
// example function function greet(name) { alert('Hello, ' + name + '!'); } // pass in the function first, // followed by all of the variables to be passed to it // (0, 1, 2, etc; doesn't matter how many) function doStuff2() { var fn = arguments[0], vars = Array.prototype.slice.call(arguments, 1); return fn.apply(this, vars); } // alerts "Hello, Chris!" doStuff2(greet, 'Chris');
source share