Categories:

onLoad Event handlers

The onload event handler is used to call the execution of JavaScript after a page, frame or image has completely loaded. It is added like this:

<body onload="inform()"> //Execution of code //will begin after the page has loaded.
<frameset onload="inform()"> //Execution of code //will begin after the current frame has loaded.
<img src="whatever.gif" onload="inform()"> //Execution of code will begin after the image has loaded.

Lets see an example of an onload handler:

<html>
<head><title>Body onload example</title>
</head>
<body onload="alert('This page has finished loading!')">
Welcome to my page
</body>
</html>

Depending on the complexity or nature of your JavaScript, sometimes it is necessary to wait until the entire page has loaded before that script is run to prevent any potential problems or make sure the aspect of the page you're manipulating has been downloaded. This is where the onload event handler comes in handy.

Executing a JavaScript function after the page has loaded is necessary sometimes