javascript multiple onload functions

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gth759k
Forum Commoner
Posts: 76
Joined: Mon Jun 15, 2009 3:04 am

javascript multiple onload functions

Post 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?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: javascript multiple onload functions

Post by pickle »

You could try wrapping that in a third, anonymous function:

Code: Select all

<body onload="function(){display('name1'); display('name2');}">
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply