...

View Full Version : Instead of using <body onload>



huy9876
06-06-2003, 04:23 AM
Hi, instead of using <body onLoad="goforit()"> command to call up a script that runs at Page load up. Is there another command that I can put into the <Script> </Script> section to run upon page load..? Thanks.

cheesebagpipe
06-06-2003, 04:27 AM
Two choices:

Pointer:

function onload_function() {
//statements
}

onload = onload_function; //no parentheses

...or literal:

onload = function() {
//statements
}

Either one, inside a <script></script> block (or external .js file), anywhere in your page..

Vladdy
06-06-2003, 04:44 AM
Not a good idea to do it this way. That's how get scripts that do not work together.

cheesebagpipe
06-06-2003, 04:54 AM
There's a simple solution to that: understand what's going on in your page!

Kor
06-06-2003, 10:53 AM
If u have two ore more scripts whose functions are called on onload (if this is your problem...?), you can do it in 3 methods:

1. <body onload ="function1();function2();function3()">

2. put al the onload function in one of the scripts (say script1), like this:

.
.
.
onload = init() {
function1();
function2();
function3();
}

or another syntax is

onload = init;
function init() {
function1();
function2();
function3();
}


!! but don't forget to erase the onload functions from the other 2 scripts (in our case, script 2 and script2) !!

3. use the same function as above, but put it in a separate JS file, callled, say, onload.js and called that JS file from head,

<script language="JavaScript" src="script/onload.js"></script>

You can run now a different number of scripts with functions called on onload...

Choopernickel
06-06-2003, 03:11 PM
I'm working on a cross-browser event queue manager, whixch I've already got an alpha done for onLoad. Would anybody like to see what's there so far?

Kor
06-06-2003, 04:49 PM
i haven't got your point... What is that about, precisely?

Choopernickel
06-06-2003, 05:15 PM
basically, there's a DOM way to register events (addEventListener()), there's an IE6+ way to register events (attachEvent()), and there's a pre-standards way to register events (element.onevent=handlerFunction).

What I needed is a way to register several events to a single event, so I came up with an onLoadQueueManager script, made of a couple functions: addOnLoadHandler() and windowHasLoaded(). You can register any number events to run onLoad using the addOnLoadHandler() function, then the actual onLoad event triggers the windowHasLoaded(), which loops through the queue and executes each function.

I'm in the process of turning this into a more generic event queue manager, eligible for any element and any event; basically turning it into an event management class with a -gasp!- fully documented API.

cheesebagpipe
06-06-2003, 05:29 PM
Seen Mike Foster's api (http://www.cross-browser.com/docs/event_om.html)?

liorean
06-06-2003, 06:15 PM
Have a look at dithered by Chris Nott: <http://www.dithered.com/javascript/dom2_events/index.html>

Choopernickel
06-06-2003, 07:51 PM
Hm. Just when I decide to undertake an arduous JavaScript journey, I find it's already been taken and conquered by others, actual pioneers. Damn this nasty habit I have of waiting until I *need* something to actually going and putting it together!

Thanks for the links, cheese & liorean. These look like just what I was about to kill myself trying to build.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum