guys hey. im trying to learn javascript, and i have a question on something thats throwing me. it relates to the order in which its written. its probably very obvious and simple, but i cant see it.
in the code below, why is - window onload - at the bottom? i understand why the js links are at the bottom of a html page, but not why this seemingly reverse order is used in a .js file.
any and all advice appreciated, thanks for reading.
In JavaScript the order of declarations is important, so functions should be defined before they are assigned to a variable or called. However, for your particular code-example, this requirement may be side-stepped because your call to preparePage() is within a closure, so you could switch the order and it should still work - try it. Good description here.
Personally I don't often encounter an issue with this as I always (where possible) define my functions before I call them. It is also a custom, or tradition, to define functions before the assignment to 'window.onload'.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
In this case the window.onload could be put anywhere in your script because all you are doing is assigning something to the browser window's onload event handler. That something (in this case an anonymous function) does not get executed until the browser triggers the window's onload event - ie. the page has finished loading completely, including downloading all the images and loading all the javascript. When the onload event is triggered, the event's handler (anonymous function) calls the preparePage() function and so preparePage() is executed after the window has finished loading.
If you did
Code:
window.onload = preparePage; //this is a valid js statement
then the function preparePage would have to be defined before the window.onload because in this case you are assigning a reference to a function and the function needs to be first defined before you can assign references to it to anything.
window.onload = preparePage; //this is a valid js statement
then the function preparePage would have to be defined before the window.onload because in this case you are assigning a reference to a function and the function needs to be first defined before you can assign references to it to anything.
Functions do *NOT* have to be defined before you make a *REFERENCE* to them. JavaScript compiles all functions before it attempts actually USE the references.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
<html>
<head>
<script type="text/javascript">
window.onload = doit;
function doit( )
{
setTimeout( doit2, 5000 );
}
function doit2( )
{
document.body.style.backgroundColor = "lightblue";
}
</script>
</head>
<body>
See? Just wait a few seconds...
<br/><br/>
JavaScript is more flexible than you think.
</body>
</html>
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Functions do *NOT* have to be defined before you make a *REFERENCE* to them. JavaScript compiles all functions within the same script before it attempts actually USE the references.
OK, bullant?
__________________
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
window.onload = doit;
var doit = function() {
...
}
So function doit() is not equivalent to var doit = function()
Correct. When you assign a function to a variable, the rules are different.
But why do any of this? Use "unobtrusive JavaScript" as I showed in post #4 then you don't NEED to make any forward references. You don't even need the onload code to be its own function.
It's cleaner, simpler, and list to Felgall: He has a host of other reasons to do it that way.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
It comes down to personal preference of how you want to code. Me, my preference is to put all the js in the <head> section unless it must go elsewhere.
I can't be bothered scrolling down to the bottom of web pages looking for javascript and unless you have a huge amount of javascript then any potential gain in page loading time by putting your js at the bottom of the page won't be noticeable to the naked eye.