Question about how functions load using document.onload

I have the following code appended at the bottom of my markup stack (like a good little web designer :D)…


document.onload = getElements();

var delay = 250;
var open = '';

function cog_slider(element){
	if(open == element)
		element = '';
	setTimeout("animate(" + new Date().getTime() + "," + delay + ",'" + open + "','" + element + "')", 33);
	open = element;
}

“cog_slider” is nothing more than a pilot function used for a menu animation process. It’s not too complex, but the thing I’m going cwazy about here is how the variables outside the function definition are being processed (variables “delay” and “open”)…

In my mind, I keep thinking that global variables should have something indicating such, but these variables “delay” and “open” lack this. Are these global variables, then? I’m familiar with seeing things like the word “global” in PHP and such, but is this unnecessary in JavaScript across the board or is this specific case I bring to light here something of a more unorthodox scenario? As you can tell, I’m not the sharpest knife in the drawer when it comes to JavaScript.

But lastly, when functions are slated or initialized through the use of document.onload, does this mean that anything between the actual onload declaration and the function definition itself gets used only once if the function definition has a setTimeout call in it (combined with the document.onload)? The reason I’m asking this is because I’m trying to figure out a way to encapsulate each function, and so far, I’m unable to do that because variables “open” and “delay” seem to be dependent on being processed prior to the function definition and the reason I wish to encapsulate everything is so that I can say I’m awesome. Well, that, and to make life easier down the road with other functions… :smiley:

Any light you pros around here can shine on this is very appreciated.

How on earth can a variable not prefixed with var but defined inside a function be global!? Is this something exclusive to JavaScript? Is there a rationale behind this phenomenon or is this just something that Brendan Eich et al thought would add spice to a programmer’s life…?

Just seems really odd to me. :goof:

why not just try it and then let :google: be your friend :slight_smile:

 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script  type="text/javascript">
 
function myFunction() {
    str = 'hello';
}
 
myFunction();
 
alert(str);
 
</script>
</head>
<body>
</body>
</html>

any variable declared outside of a function will be global.

any variable, prefixed with var, declared within a function will be local to that function only.

any variable, not prefixed with var, declared within a function will be global.

example

 
<script type="text/javascript">
 
var myVar1 = 0;  //global variable
 
function myFunction() {
     var myVar2 = 23;  //local variable to this function only
     myvar3 = 56;  //global variable
}
 
</script>