Possible Duplicate:
How to execute a JavaScript function when I have its name as a string
I have this function
function myfunc() {
alert('it worked!');
}
and this variable which is a string
var callback = 'myfunc';
How do I trigger the function from that string?
Assuming that the function is declared globally it will be contained in the
windowobject so you can call it usingwindow[callback]().Globally declared functions are stored in the
windowobject so you would be able to referencemyfuncusingwindow.myfunc. As with other properties on JavaScript objects you can use the brace syntax instead of the dot notation which would bewindow["myfunc"]since you have the string"myfunc"contained in yourcallbackvariable you can simply use that instead. Once you have the reference you call it as a function which giveswindow[callback]().