Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

<body onload="doSomething()"> doesn't work

Status
Not open for further replies.

AvaB

Programmer
Jun 23, 2004
16
CA
i have a main.jsp file whose <body onload="doSomething()"> works perfectly on its own.
however, if i instead call AAA.jsp which includes/embeds 2 jsp files (main.jsp and menu.jsp), the onload of main.jsp no longer works. i suspect it is because AAA.jsp also has its own onload function and for some reason this keeps main.jsp's onload from executing.

my questions:

- would there be any other reasons i should watch out for why the 2nd onload is not
working? perhaps my initial assumption is wrong and something else is the problem.
- i don't have edit access to AAA.jsp so i cant just move both onloads onto
main.jsp. besides, there are other files dependent on AAA.jsp so editing it
wouldnt be a good idea. that being said, would there be another way i can simulate
the onload function without duplicating the <body onload=""> tag?

thanks.
 
I believe that you can write a window.onload function call in the main.jsp script.

<script>
window.onload = pageLoaded()

function pageLoaded(){

}
</script>

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)

zen.gif
 
Thanks for your suggestion. I tried it but no luck. If I do it this way...
Code:
<body onload="test1()">
<p>Paragraph 2</p>
</body>
<SCRIPT LANGUAGE="JavaScript">

function test1(){
	alert("popup 1");
}
window.onload = function test2(){
	alert("popup 2");
}
</script>
<body>
<p>Paragraph 1</p>
</body>
... function test(2) executes but the other one does not. I'm trying to figure out a way so both <body> onloads are called.
 

You cannot have 2 onload events - it is just not possible, as one will always override the other.

The only way around this is to call one function from the other.

Hope this helps,
Dan
 
Another idea: make "global loader":
Code:
<html>
<head>
<script language="javascript">
var aInitInfo = [];
function addInit( sCode )	{ aInitInfo.push( sCode ) }

function pageInit()
{	for (i=0; i< aInitInfo.length; i++)
		eval( aInitInfo[i] );
}
</script>

</head>
<body onload="pageInit()">
That way you may dynamically "add" as many onload functions you need. Sample code (... continued):
Code:
div span blah blah...
<script language="javascript">
function onload1()
{	document.body.style.backgroundColor = "#ccf3d0";
}

function onload2()
{	alert( "Loaded" );
}

</script>

<script language="javascript">
addInit( "onload1()" )
</script>

<script language="javascript">
addInit( "onload2()" )
</script>

</body>
</html>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top