The other way to achieve the same result is by using onload inside body tag of the page. You can read this article on how to keep the onload event handler to set the focus on input tag once the page loads.
Let us start some simple example of uses of window.onload function where we will prompt a message box once the page loads. Here is the script
We can also achieve the same result by using onload event of body tag. Then what is the difference ?
To keep the userid textbox on focus ( time of loading of page )
Difference between window.onload function and body tag onload.wondow.onload is executed when full page is loaded to the browser ( including all images, style , scripts ,CDN etc )
document.onload is executed when DOM is ready ( Not necessarily all connected script, images etc to be downloaded )
We can't keep any code to access the property set or any other related objects of the elements present in body of the page by using window.onload function. Say we want to select the text present inside a text box as soon as the page loads. This is not possible by using window.onload event but this is possible by using body onload command. The code below will generate error message.
This article is written by plus2net.com team.