Click to See Complete Forum and Search --> : nested functions
wynton_ca
10-30-2003, 05:57 PM
Im writing some code that has two parts, an onclick event and an onmouseover event, which both work off one another. However the only way i can see to implement would be to utilize a nested function. Here's what Im specifically asking about. Consider the code below:
function one(){
execute.some.block.of.code;
function two(){
execute.some.block.of.code;
}
}
[Somewhere on the page]
<a href="#" onmouseover="one().two()" onclick="one()">foo</a>
Can I call a nested function outside of the parent function? Is there a way around this?
Poryhedron
10-30-2003, 07:00 PM
Well, you can call a function from within another function...would something like this work for you?
function minor() {
DoStuff;
}
function major() {
DoMoreStuff;
minor();
DoStillMoreStuff;
}
<a href="#" onmouseover="minor()" onclick="major()">foo</a>
dragle
10-31-2003, 08:45 AM
Hiya,
As you have it defined, you can only refer to "two" from within "one," i.e., think of "two" as being like a variable that is local to "one". You can, however, call "two" from outside of "one," provided you assign "two" to something that itself is outside of "one" (so that you can still refer to it). A global variable, or object handler, for example. This may defeat the purpose of whatever you're trying to accomplish, though. For example:
function outerFunction(e) {
if(!e) var e={pageX:0,pageY:0};
var xPos=(typeof(e.pageX)!="undefined") ? e.pageX : e.clientX;
var yPos=(typeof(e.pageY)!="undefined") ? e.pageY : e.clientY;
innerFunction=function() {
alert("Last Click was at x="+
xPos+" y="+yPos);
return false;
};
return false;
}
outerFunction();
...
<a href="somelink.html"
onmouseover="return innerFunction()"
onclick="return outerFunction(event)">Click Me</a>
HTH,