Idocs Guide to HTML

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:


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!');

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

<BODY onLoad="init()">

Copyright 1997-2002 Idocs Inc. Content in this guide is offered freely to the public under the terms of the Open Content License and the Open Publication License. Contents may be redistributed or republished freely under these terms so long as credit to the original creator and contributors is maintained.