Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic Functions 1

Status
Not open for further replies.

bdichiara

Programmer
Oct 11, 2006
206
US
Is there a way to run a function dynamically. For instance:

If I have a function: validatePage()

and I also have a function: savePage()

Is it possible to do something like:

function doAction(action_type){
{action_type}+Page();
}

Then I could do: onClick="doAction('validate');"

which would run:

doAction('validate'){
validatePage();
}

I hope this make sense. Let me know if you have a question.

_______________
_brian.
 
Yup, here's the syntax:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<title>title test</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<script type="text/javascript">

function blah() {
   alert("I'm the blah function");
}

function blah2() {
   alert("I'm the number 2 blah function");
}

function dynamicFunction([!]fn[/!]) {
   [!]fn()[/!];
}

//run the functions when the page loads:
window.onload = function () {
   dynamicFunction([!]blah[/!]);
   dynamicFunction([!]blah2[/!]);
};


</script>
<style type="text/css"></style>
</head>
<body>

</body>
</html>

-kaht

Looking for a puppy? [small](Silky Terriers are hypoallergenic dogs that make great indoor pets due to their lack of shedding and small size)[/small]
 
but how do i string the name together?

like:
Code:
function blah() {
   alert("I'm the blah function");
}

function blah2() {
   alert("I'm the number 2 blah function");
}

function blah3() {
   alert("I'm the number 3 blah function");
}

function dynamicFunction(fn) {
   fn();
}

function alertBlah(num){
   if(num == "1"){
      dynamicFunction(blah);
   } else {
      [blue]dynamicFunction('blah'+num)[/blue]
   }
}

_______________
_brian.
 
use the window collection, it contains reference to all the defined functions:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<title>title test</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<script type="text/javascript">

function blah1() {
   alert("I'm the blah function");
}

function blah2() {
   alert("I'm the number 2 blah function");
}

function dynamicFunction(fn) {
   fn();
}

//run the functions when the page loads:
window.onload = function () {
   var i = 1;
   dynamicFunction([!]window["blah" + i][/!]);
   i = 2
   dynamicFunction([!]window["blah" + i][/!]);
};


</script>
<style type="text/css"></style>
</head>
<body>

</body>
</html>

-kaht

Looking for a puppy? [small](Silky Terriers are hypoallergenic dogs that make great indoor pets due to their lack of shedding and small size)[/small]
 
That worked. Thanks a bunch kaht!

_______________
_brian.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top