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
There is no difference, but you should not use either.
In many browsers, the window.onload
event is not triggered until all images have loaded, which is not what you want. Standards based browsers have an event called DOMContentLoaded
which fires earlier, but it is not supported by IE (at the time of writing this answer). I'd recommend using a javascript library which supports a cross browser DOMContentLoaded feature, or finding a well written function you can use. jQuery's $(document).ready()
, is a good example.