Learn HTML Code Tutorial Reference Guide

Running Two (or more) Commands With onLoad

A common question we get is how to run two different commands with <BODY onLoad="...">. It's pretty simple: just put both of them in the onLoad attribute with a semicolon between them. It also might be simpler to put the commands in a separate function and call the function in onLoad.

For example, suppose you want to run this command when the document is loaded:

alert('Hello There!')

and you also want to run this command:

alert('Howdy!')

You can do this by putting them both in the onLoad attribute separated by a semicolon:

<BODY onLoad="alert('Hello There!'); alert('Howdy!')">

If you want to run more than two commands, just put semicolons in between all the commands. However, this tends to get messy, so it's usually a good idea to put the commands in a single function and call the function from onLoad. So, for example, you could create a function called init() by putting this script in the <HEAD> section of your page:

<SCRIPT TYPE="text/javascript">
function init()
{
alert('Hello There!');
alert('Howdy!');
}
//-->
</SCRIPT>

Then you can call init() with this onLoad attribute:

<BODY onLoad="init()">

 
HOSTING / DESIGN
MAKE MONEY

Home
  |   Tutorials   |   Forum   |   Quick List   |   Webmaster Directory   |   About
Copyright ©1997-2002 Idocs and ©2002-2007 HTML Code Tutorial