I want execute JavaScript function which the name is coming as a string dynamically. I don't need to pass any parameters while executing the function.
Please can any one guide me how to achieve this?
I want execute JavaScript function which the name is coming as a string dynamically. I don't need to pass any parameters while executing the function.
Please can any one guide me how to achieve this?
If the function is global, you should do window[funcName]() in browser.
Perhaps a safer way is to do something like this (pseudo code only here):
function executer(functionName)
{
if (functionName === "blammo")
{
blammo();
}
else if (functionName === "kapow")
{
kapow();
}
else // unrecognized function name
{
// do something.
}
You might use a switch statement for this (it seems like a better construct):
switch (functionName)
{
case "blammo":
blammo();
break;
case "kapow":
kapow();
break;
default:
// unrecognized function name.
}
You can optimize this by creating an array of function names, searching the array for the desired function name, then executing the value in the array.