...

View Full Version : combining more than one 'on load' tag in body



oleg2
12-10-2009, 03:40 AM
hey i just have a simple question... how can i do more than one "on load" tag in the body... this is what i have so far, i know you can't have more that one body tag but i want to combine the on loads...

<body onload="menuSlider.init('menu','slide')">
<body onload="changePhoto(0)">


thanks

Iszak
12-10-2009, 09:05 AM
I believe you'd do something along the line of.


<body onload="menuSlider.ini('menu', 'slide'); changePhoto(0);">

But don't quote me, because I usually use event listeners e.g. in jQuery.


$(function(){
// code
});

// Or
$(document).ready(function(){
// code
});


Undoubtedly you could also do


$('body').load(function(){
// code
});


That said you should try and use event listeners, it's much easier to manage in my opinion and furthermore it's separating the layers (content, presentational and finally behavioral)

Fumigator
12-10-2009, 04:21 PM
I agree with Iszak, and especially since you posted this question in the jQuery forum, may as well take advantage of jQuery's awesomeness.

But if you really really want to use Javascript only, here's a function I found a while ago that makes it easy to kick off two functions with the onload event.



function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
if (oldonload) {
oldonload();
}
func();
}
}
}


Then you simply call this addLoadEvent() function multiple times.



addLoadEvent (doSomething);
addLoadEvent (doSomethingElse);


I found the original page I stole this from, it has all the info you need to get it working properly.

http://www.htmlgoodies.com/beyond/javascript/article.php/3724571

oleg2
12-10-2009, 06:23 PM
thanks a lot fellas it worked... yeh the only reason im doin it in javascript is that its for an assignment and not allowed to use jquery

hdewantara
12-11-2009, 10:12 PM
How about using addEventListener() ->http://www.w3.org/TR/DOM-Level-2-Events/events.html?

But this only to browsers with DOM2 supported (as usual... IE use other non-standard method) and should remove it using removeEventListener() later?

Regards,



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum