...

View Full Version : A way to replace body onload



FutureSpy
08-14-2005, 04:47 AM
Hi.

I'm trying to port a standalone GuestMap script to XOOPS (it's a PHP CMS). The problem comes when I try to run it into the page. The original script starts a function on body (
<body onload=init()>), but since I'm running into XOOPS template page, I can't have 2 body tags.

Are there a way I can replace body onload?


I already tried
<script type="text/javascript">init()</script> and
<script type="text/javascript">onload=init()</script> but I'm unable to get the page to load properly. Can someone shed me some light?

Sorry if it's a too obvious question. I know absolutely -NO- JavaScript. :(

Thanks in advance,
-Yuji

jscheuer1
08-14-2005, 05:00 AM
Your second idea should work unless there is already another onload event for the page and as long as it is parsed after the init function and the rest of the code that the init function uses (which it should be, being onload). If the problem is a conflict with another onload event, here is the 'Swiss Army Knife' solution:

<script type="text/javascript">
if ( typeof window.addEventListener != "undefined" )
window.addEventListener( "load", init, false );
else if ( typeof window.attachEvent != "undefined" ) {
window.attachEvent( "onload", init );
}
else {
if ( window.onload != null ) {
var oldOnload = window.onload;
window.onload = function ( e ) {
oldOnload( e );
init();
};
}
else
window.onload = init;
}
</script>

FutureSpy
08-14-2005, 05:31 AM
jscheuer1, thanks a lot for your reply.

Your solution worked like a charm :thumbsup:

Thanks again! ^^

brothercake
08-15-2005, 08:54 PM
You need to add an extra conditional block *between* the window.addEventListener and window.attachEvent - for document.addEventListener, because in Opera the load event comes from document, not window.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum