...

View Full Version : multiple functions called onload



]|V|[agnus
07-04-2004, 11:32 PM
I would like to call several sort of 'init' type functions when a document loads. I understand that listing several for window.onload doesn't work because each new one overwrites the previous or something to that effect, yes?

I did some searching and it appears that to achieve this I either need to a) list functions in the <body> onload attribute or b) attach events using JavaScript or something to that effect.

I am interested in the latter because of my interest in keeping behavior seperate from the structure. From what I've been able to gather, this is a purist approach which is hard to get universal functionality for, but maybe I do not understand correctly.

I'm ultimately just looking for the smartest, simplest approach to get the desired result. Any pointers or references?

jamescover
07-04-2004, 11:40 PM
<script type="text/javascript" language="javascript" src="source.js"></script>

or

<body onLoad="javascript:myFunc1();myFunc2();myFunc3();">



-james

Willy Duitt
07-04-2004, 11:50 PM
Or:

function duitt(){
myFunc1();
myFunc2();
myFunc3();
}
window.onload = duitt;

glenngv
07-05-2004, 02:35 AM
Or:


window.onload = function(){
myFunc1();
myFunc2();
myFunc3();
}

Choopernickel
07-06-2004, 08:44 PM
Or:
function registerEvent (obj, trigger, handler) {
if (obj.addEventListener) {
obj.addEventListener(trigger, handler, false);
} else if (obj.attachEvent) {
obj.attachEvent('on' + trigger, handler);
} else {
if (!obj['on' + trigger]) {
obj['on' + trigger] = function () {
if (!this[trigger + 'handlers']) {
return false;
}
for (var ix = 0; this[trigger + 'handlers'].length > ix; ix++) {
this[trigger + 'handlers'][ix]();
}
}
}
if (!obj[trigger + 'handlers']) {
obj[trigger + 'handlers'] = [];
}
obj[trigger + 'handlers'][obj[trigger + 'handlers'].length] = handler;
}
}then:
registerEvent(window, 'load', function1);
registerEvent(window, 'load', function2);
registerEvent(window, 'load', function3);

I'm not using a for-in loop or the Array.push() method because (IIRC) they aren't supported by several browsers that require that backward-compatible multiple-handler-per-event assignment. (This is untested, and I'm sure needs refinement; it's similar to one I wrote for Netscape 6 and IE 6 pre-SP1)

]|V|[agnus
07-07-2004, 06:06 AM
heh...

i found this today while looking for information on something else. (http://www.sitepoint.com/article/1259)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum