[removed] vs <body onload=“”/>

后端 未结 13 2082
盖世英雄少女心
盖世英雄少女心 2020-11-22 13:45

What exactly is the difference between the window.onload event and the onload event of the body tag? when do I use which and how shoul

13条回答
  •  清歌不尽
    2020-11-22 14:23

    window.onload = myOnloadFunc and 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.

提交回复
热议问题