Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Web
Expertise: Intermediate
Apr 27, 1999

Call Functions by Name

JavaScript has a powerful mechanism to create and execute code on the fly using the eval() statement. This statement takes a string as a parameter. This string is then run just like the static <SCRIPT> code on the page. Because you can put any set of JavaScript statements into this string and have them execute you can also use this mechanism to call an arbitrary function--just like you would in static code. For example, this code calls a function based on the value a user enters into an text <INPUT> control:
 
<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!--
function go() {
	alert("This is the GO function");
}

function stop() {
	alert("This is the STOP function");
}
// -->
</SCRIPT>
</HEAD>
<BODY>

<FORM>
Function Name: <INPUT TYPE="TEXT" NAME="funcname"
VALUE="go"> (try "go" or "stop")<BR><BR>
<INPUT TYPE="BUTTON" VALUE="Click here to display alert"
onClick="eval(this.form.funcname.value + '()')">
</FORM>

</BODY>
</HTML>
Boris Feldman
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date