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
Think of onload like any other attribute. On an input box, for example, you could put:
Or you could call:
document.getElementById('test1').value = "somethingelse";
The onload attribute works the same way, except that it takes a function as its value instead of a string like the value attribute does. That also explains why you can "only use one of them" - calling window.onload reassigns the value of the onload attribute for the body tag.
Also, like others here are saying, usually it is cleaner to keep style and javascript separated from the content of the page, which is why most people advise to use window.onload or like jQuery's ready function.