Difference between window.onload and body.onload
There is a slight difference between the windows onload and the body onload, but before approaching the differences between them first we required the knowledge of what are those onloads. After, that we will know the usage in different scenarios of those where we will notice the slight differences between them.
onloads: The onloads event is fired when an object is fully loaded, there are two types of onload event one is windows onload and body onload.
- Windows onload: This window’s onload event fires at the start.
- Body onload: This body onload event fires once all content is downloaded, if we want to find an element and update the style or content, then, body onload is the right choice to use.
Note: If update the style and content do not bother you then it does not matter which onload you are using.
The below example illustrates the differences between windows onload and body onload:
- Window onload:
- Body onload: