XHTML » body » onload


The onload event occurs when the HTML document is loaded into a window for viewing. When the event happens, the code calls a function that performs a desired action. For example, you may want an alert box to appear that provides useful information to the user.

It can be used with either the body or frameset tags.
In contrast, the onunload event occurs just before the HTML document is unloaded or removed from viewing.


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<title>DevGuru Test Form</title>
<script type="text/javascript">
function checksubmit()
   if (document.formname.fullname.value == "")
      alert("Please enter your full name")
      return false
   if (document.formname.emailaddress.value == "")
      alert("Please enter your email address")
      return false
   return true
<body onload="document.formname.fullname.focus()">
<br /><br />
Please leave one or both of the required fields blank and click the submit button.
<br />
<hr />
<br />
If you wish to receive information about upgrades to dgCharge,<br />
please fill out this form.
<br /><br />
<form method="post" name="formname" action="html_form_example.asp" onsubmit="return checksubmit()">
Full Name (required)       
<input type="text" name="fullname" size="30" />
<br /><br />
Email Address (required)
<input type="text" name="emailaddress" size="30" />
<br /><br />
Phone Number (optional)
<input type="text" name="phonenumber" size="15" />
<br /><br />
<input type="submit" name="submitbtn" value="Submit" />
<input type="reset" value="Clear" />

In this form example, the onload event is used in the body tag to set the focus to the Full Name input box.

Language(s): XHTML