Home >Web Front-end >JS Tutorial >Implementation method of JavaScript calling function through string_javascript skills

Implementation method of JavaScript calling function through string_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:08:411810browse

The example in this article describes the implementation method of JavaScript calling functions through strings. Share it with everyone for your reference. The specific analysis is as follows:

In JavaScript, we can call a function based on a string of function names, so that we can implement dynamic function calls. We only need to pass the name of a function to call the function.

Copy code 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 calling example
Copy code The code is as follows:





<br>
Function fnFooBar(strVal) {<br>
alert(strVal);<br>
              return 1;<br>
}<br>



<script><br> function fnFooBar(strVal) {<br> alert(strVal);<br> Return 1;<br> }<br> function call() {<br> var strFunctionName = document.getElementById("functionName").value;<br> var strFunctionParam = document.getElementById("functionParam").value;<br> var fn = window[strFunctionName]<br> var ret = fn(strFunctionParam);<br> }<br> </script>

I hope this article will be helpful to everyone’s JavaScript programming design.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn