Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help debugging this rollover script.. Thanks.

Status
Not open for further replies.

JasonBleers

Technical User
Aug 31, 2000
1
US
Here's Javascipt my code:
<!--

/* Inactive images */
img1on = new Image();
img1on.src = &quot;files/image1on.gif&quot;;
img2on = new Image();
img2on.src = &quot;files/image2on.gif&quot;;
img3on = new Image();
img3on.src = &quot;files/image3on.gif&quot;;
img4on = new Image();
img4on.src = &quot;files/image4on.gif&quot;;
img5on = new Image();
img5on.src = &quot;files/image5on.gif&quot;;
img6on = new Image();
img6on.src = &quot;files/image6on.gif&quot;;

/* Active images */
img1off = new Image();
img1off.src = &quot;files/image1off.gif&quot;;
img2off = new Image();
img2off.src = &quot;files/image2off.gif&quot;;
img3off = new Image();
img3off.src = &quot;files/image3off.gif&quot;;
img4off = new Image();
img4off.src = &quot;files/image4off.gif&quot;;
img5off = new Image();
img5off.src = &quot;files/image5off.gif&quot;;
img6off = new Image();
img6off.src = &quot;files/image6off.gif&quot;;

/* Image On function */
function imgOn(imgonName) {
if (document.images) {
document
.src = eval(imgonName + &quot;on.src&quot;);
}
}

/* Image Off function */
function imgOff(imgoffName) {
if (document.images) {
document
.src = eval(imgoffName + &quot;off.src&quot;);
}
}

/* All Images Off function */
function allOff() {
if (document.images) {
for(i=1; i<6; i++) {
tempImg = eval(&quot;img&quot; + i);
document[tempImg].src = eval(tempImg + &quot;off.src&quot;);
}
}
}
//-->

The following link works fine:
<!--
<A href=&quot;?&quot; onmouseout=&quot;imgOff('img1');&quot; onmouseover=&quot;imgOn('img1')&quot;><IMG alt=&quot;&quot; border=0 height=28 name=&quot;img1&quot; src=&quot;files/image1off.gif&quot; width=106></A>
-->

The following link doesn't:
<!--
<A href=&quot;?&quot; onmouseout=&quot;imgOff('img1');&quot; onmouseover=&quot;allOff()&quot;><IMG alt=&quot;&quot; border=0 height=28 name=&quot;img1&quot; src=&quot;files/image1off.gif&quot; width=106></A>
-->

Whenever the allOff function is called I recieve and error stating that 'img1 is not defined'. Can anybody tell me why?

I really need a function that returns all the images to the off state. Your help is much appreciated.
 
Remove the eval call from this:

tempImg = eval(&quot;img&quot; + i);

Should be this:

tempImg = &quot;img&quot; + i;

You're just concatonating strings here (javascript will convert 'i' to a string automatically), so no need for eval().

You could do also make it a little shorter by doing this instead:

document.images[&quot;img&quot; + i].src = eval(tempImg + &quot;off.src&quot;);

You needed eval() for the last part because you're converting a string into a variable. Of course, the winking smiley face is optional.

Cheers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top