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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…