The implementation method of the function called by a string in JavaScript.
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.
Copy codeThe 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:
Copy codeThe 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.