Page 1 of 1

javascript multiple onload functions

Posted: Thu Dec 10, 2009 2:45 pm
by gth759k
I'm trying to pass the same function twice in the body onload tag with different variables like this:

<body onload="display('name1'); display('name2');">

where display(param) looks like

Code: Select all

 
function display(param) {
     document.getElementById(param).innerHTML = param;
}
 
and the file has divs:

<div id="name1"></div>
<div id="name2"></div>

so that the phrase passed through display() gets put in a div of the same id as the param.

It works fine with just one funtion i.e. <body onload="display('name1');"> but when I use php to dynamically add more display() functions to the onload and divs to the page I get a bunch of crap. Sometimes the name2 gets put in <div id="name1"></div>. Any suggestions?

Re: javascript multiple onload functions

Posted: Thu Dec 10, 2009 4:20 pm
by pickle
You could try wrapping that in a third, anonymous function:

Code: Select all

<body onload="function(){display('name1'); display('name2');}">