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
should override window.onload.
With
, document.body.onload might be null, undefined or a function depending on the browser (although getAttribute("onload") should be somewhat consistent for getting the body of the anonymous function as a string). With window.onload, when you assign a function to it, window.onload will be a function consistently across browsers. If that matters to you, use window.onload.window.onload is better for separating the JS from your content anyway. There's not much reason to use
anyway when you can use window.onload.In Opera, the event target for window.onload and
(and even window.addEventListener("load", func, false)) will be the window instead of the document like in Safari and Firefox. But, 'this' will be the window across browsers.What this means is that, when it matters, you should wrap the crud and make things consistent or use a library that does it for you.