How to use JavaScript to call functions using strings

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.