Forum Moderators: open
In fact I am not sure if the title is proper. I will give you an example on my question:
Originally:
this.ajaxFunction();
I want to use:
var func_name="ajaxFunction()" ;
Then I want to call ajaxFunction() by use of the variable func_name. How could I do? I am new for Javascript. Is there some experts help me about it? Thanks.
I want to use:var func_name="ajaxFunction()" ;
Then I want to call ajaxFunction() by use of the variable func_name. How could I do?
You could assign the variable to contain the actual function itself. For example:
var func_name=ajaxFunction;
Note, no quotes, and no ().
Here's an example:
function ajaxFunction() {
alert("hello");
}
var func_name=ajaxFunction;
func_name();
Alternatively, you could use the eval function to evaluate the string into JavaScript code. For example:
function ajaxFunction() {
alert("hello");
}
var func_name="ajaxFunction()";
eval(func_name);
This method is slightly more costly though.