View Full Version : multiple functions called onload

07-05-2004, 12:32 AM
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?

07-05-2004, 12:40 AM
<script type="text/javascript" language="javascript" src="source.js"></script>


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


Willy Duitt
07-05-2004, 12:50 AM

function duitt(){
window.onload = duitt;

07-05-2004, 03:35 AM

window.onload = function(){

07-06-2004, 09:44 PM
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;
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)

07-07-2004, 07:06 AM

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