window.onload vs

window.onload = myOnloadFunc and <body onload="myOnloadFunc();"> are different ways of using the same event. Using window.onload is less obtrusive though – it takes your JavaScript out of the HTML.

All of the common JavaScript libraries, Prototype, ExtJS, Dojo, JQuery, YUI, etc. provide nice wrappers around events that occur as the document is loaded. You can listen for the window onLoad event, and react to that, but onLoad is not fired until all resources have been downloaded, so your event handler won’t be executed until that last huge image has been fetched. In some cases that’s exactly what you want, in others you might find that listening for when the DOM is ready is more appropriate – this event is similar to onLoad but fires without waiting for images, etc. to download.

Leave a Comment