If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Enjoy an ad free experience by logging in. Not a member yet? Register.

question about the order script is written in

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.

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.

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.