/    Sign up×
Community /Pin to ProfileBookmark

<body onload> cancelling out other javascripts

I have multiple scripts running on my site, and for my dropdown menu system, I require a:
<body onload=”functionname();”>

however, having this ‘onload’ function in the body tag is causing another small script (specific to one individual page) to not function at all. is there a simple way to make sure that my second script still loads and the initial onload script is not effected?

thanks in advance for any help provided
j

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@felgallMar 13.2008 — You can only have one call to window.onload in a web page - either in the JavaScript or in the body tag. Where you have multiple things to run at that time you need to combine them in the one call.
Copy linkTweet thisAlerts:
@sirtcarlosauthorMar 13.2008 — Stephen,

Thanks for getting back to me. I guess consider me a javascript rookie on this, cause I'm still confused.

Basically I have two things going on.

First, I have my dropnmenu system developed in CSS but there is also a Javascript1.2 file associated with this. For this file, I added: <body onload="assignHoverNav();">

Now I am building an image slideshow that is also powered by CSS and Javascript. The code is in a separate .js file (slideshow.js) from the menu script (scripts.js). I include all the scripts in the <head> properly, but the slideshow does not work. I found when the onload was taken out, the slideshow works fine (but obviously the dropdowns then do not).

How do I either combine functions as you mention? Are you saying, basically take all the code from the slideshow.js and then put it below the dropdown code, and then wrap the entire section and call that onload?

Is there anything I can put in the <body> that allows the page to continue running all scripts?

Sorry for the long-winded way I explained this, but hopefully it makes sense.

Thanks again for any help.
Copy linkTweet thisAlerts:
@Declan1991Mar 13.2008 — In the slideshow code, there is a line similar to this:[code=php]
window.onload = xxx;
[/code]

Find it, take the xxx bit and add the function to your onload code. SO:[code=php]
<body onload="functionName();xxx();">
[/code]
Copy linkTweet thisAlerts:
@TheBearMayMar 13.2008 — I've started using the code below to keep from overwriting the onload event unless I know I won't be doing multiple onload events.

<i>
</i>if (window.addEventListener)
window.addEventListener("load", dothis, false);
else
window.attachEvent("onload", dothis);
Copy linkTweet thisAlerts:
@Declan1991Mar 13.2008 — [code=php]function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
if (oldonload) {
oldonload();
}
func();
}
}
}
window.addLoadEvent(functionname);[/code]
is also very good.
×

Success!

Help @sirtcarlos spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...