You are here: HTML Codes > Document Tags > <BODY ...> > <BODY onLoad="...">
previous page<BODY onLoad="..."><BODY onUnload="..."> >>>

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 youalso 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()">

<BODY onUnload="..."> >>>

Copyright © 2002-8 Art Dacor USA LLC. All Rights Reserved.
3727 West Magnolia Blvd #489, Burbank, CA, 91505, USA.
www.HTML-HTML.com