/    Sign up×
Community /Pin to ProfileBookmark

Having trouble using multiple scripts on one page

Hi,

I am trying to feature a select-and-go navigation script and also a mouseOver script on the same page.

Here is the page:

[url]http://www.scottfennell.com/mof/index1.php[/url]

Expected behavior:
You roll your mouse over the small photos to see a blown-up version. You select a recent project from the drop down menu to go to that project’s page.

Actual behavior:
Whichever script I call second in the head of my html page gets executed. Whichever one I call first does not.

HTML head section:

[CODE]<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml” xml:lang=”en” lang=”en”>
<head>
<title>Matter of Fact Maintenance</title>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″ />
<link rel=”icon” href=”favicon.ico” type=”image/x-icon” />
<link rel=”shortcut icon” href=”favicon.ico” type=”image/x-icon” />
<link rel=”stylesheet” type=”text/css” href=”mofstyle.css” />
<script type=”text/javascript” src=”scriptprojects.js”></script>
<script type=”text/javascript” src=”selectandgo.js”></script>

</head>[/CODE]

mouseOver script:

[CODE] window.onload = rolloverInit;

function rolloverInit() {
for (var i=0; i<document.links.length; i++) {
var linkObj = document.links[i];
if (linkObj.className) {
var imgObj = document.getElementById(linkObj.className);
if (imgObj) {
setupRollover(linkObj,imgObj);
}
}
}
}

function setupRollover(thisLink,textImage) {
thisLink.imgToChange = textImage;
thisLink.onmouseover = rollOver;

thisLink.outImage = new Image();
thisLink.outImage.src = textImage.src;

thisLink.overImage = new Image();
thisLink.overImage.src = thisLink.id + “b.jpg”;
}

function rollOver() {
this.imgToChange.src = this.overImage.src;
}[/CODE]

seclect-and-go script:

[CODE]window.onload = initForm;
window.onunload = function() {};

function initForm() {
document.getElementById(“newLocation”).selectedIndex = 0;
document.getElementById(“newLocation”).onchange = jumpPage;
}

function jumpPage() {
var newLoc = document.getElementById(“newLocation”);
var newPage = newLoc.options[newLoc.selectedIndex].value;

if (newPage!= “”) {
window.location = newPage;
}
}[/CODE]

My theory: Both functions are called with the window.onload event, and the first one gets forgotten about when the second one is encountered.

I am very much a novice and don’t really know what my alternative is.

Thank you!

Scott

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@ZeroKilledMay 17.2009 — you have located where is the problem! whichever script come second will overwrite the handler registered on event onload by the first script. to solve it, you can use a somewhat more advanced model for registering event. this model get rid about the overwriting thing. however, the problem is that one of the main browser (MSIE) use a somewhat different model than standard specify. thankfully, with a try/catch you can get rid of it.

<i>
</i>try{ // always try standard first;
addEventListener('load', initForm, false);
addEventListener('load', rolloverInit, false);
} catch(e){ // so far MSIE browser is the only one that doesn't use [b]addEventListener[/b];
attachEvent('onload', initForm);
attachEvent('onload', rolloverInit);
}
Copy linkTweet thisAlerts:
@scottfennellauthorMay 19.2009 — I cut and pasted your code at the top of both of my scripts, and now they both work together, as intended. Problem solved!
×

Success!

Help @scottfennell 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 5.13,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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