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
Release: 2016-05-16 16:08:41
Original
1826 people have browsed it

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:





1

2

3

4

5

<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.

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template