How to use JavaScript to call functions using strings
This article mainly introduces the implementation of JavaScript function calling through strings. The example analyzes the javascript function calling techniques dynamically. For more information, see
This example describes how to call functions using strings in JavaScript. 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:
<Input type = "text" id = "functionName" name = "functionName" size = "20" value = "fnFooBar">
<Input type = "text" id = "functionParam" name = "functionParam" size = "30" value = "Happy New Year .!! ">
<Input type = "button" style = "font-weight: bold" value = "Call" onclick = "javascript: call ();">
<Br>
<Pre>
Function fnFooBar (strVal ){
Alert (strVal );
Return 1;
}
</Pre>
<Br>
<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.