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

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)">


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.

// code

// Or
// code

Undoubtedly you could also do

// 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)

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) {

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.


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

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?