This article mainly introduces how to call functions using strings in JavaScript. The example shows how to use javascript to call functions dynamically, for more information about how to call functions using strings in JavaScript, see the example in this article. Share it with you for your reference. The specific analysis is as follows:
In JavaScript, we can call a function based on the string of the function name. in this way, we can call a dynamic function by passing the name of a function.
The code is as follows:
Var strFun = "someFunction"; // Name of the function to be called
Var strParam = "this is the parameter"; // Parameters to be passed in function
// Create the function
Var fn = window [strFun];
// Call the function
Fn (strParam );
The following is a detailed call instance:
The code is as follows:
function fnFooBar(strVal) {
alert(strVal);
return 1;
}
Script
Function fnFooBar (strVal ){
Alert (strVal );
Return 1;
}
Function call (){
Var strFunctionName = document. getElementById ("functionName"). value;
Var strFunctionParam = document. getElementById ("functionParam"). value;
Var fn = window [strFunctionName]
Var ret = fn (strFunctionParam );
}
Script
I hope this article will help you design javascript programs.