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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

something wrong with my code?? - image swapping script

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
hello all, i am working on a script that rotates 4 images. i reckon i have got the idea right, useing if else statements to test which image is in which slot then modifying it accordingly. this loops round to test each image slot. but the script isnt working, and since none of the images are changing at all, i figure there must be something i have done wrong with the code. here is the code, any help would be appreciated.
thx
bob

<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
function rotateimages2(){
for(i=1;i<=4;i++){
if(document.images[&quot;top&quot;+i].src==&quot;top4.jpg&quot;){
document.images[&quot;top&quot;+i].src=&quot;top3.jpg&quot;;
}
else if(document.images[&quot;top&quot;+i].src==&quot;top3.jpg&quot;){
document.images[&quot;top&quot;+i].src=&quot;top2.jpg&quot;;
}
else if(document.images[&quot;top&quot;+i].src==&quot;top2.jpg&quot;){
document.images[&quot;top&quot;+i].src=&quot;top1.jpg&quot;;
}
else if(document.images[&quot;top&quot;+i].src==&quot;top1.jpg&quot;){
document.images[&quot;top&quot;+i].src=&quot;top4.jpg&quot;;
}
}

setTimeout(&quot;rotateimages2()&quot;,5000);
}
</SCRIPT>
<body onload=&quot;rotateimages2()&quot;>





 
If it doing nothing then it is going thru all the if conditions and finding that none of them match -meaning there is a probelem with the if() part.

I have got a suspicion it is to do with this:


[&quot;top&quot;+i]


There is some issue with concatenating these two items. You need to convert the value i has into a string. I have tried toString(i) - but this returns [object]? This is the problem now!
b2 - benbiddington@surf4nix.com
 
You lucky son of a gun!!!

I made a script like that not too long ago!
Just copy this one and adjust it to your liking!!!



<script language=&quot;JavaScript1.1&quot;>
<!--

var image=&quot;&quot;;
var banners=0;


function cycle()
{
if (++banners > 3) banners=0;
loadbanners();
document.banner1.src = image;
window.setTimeout('cycle();', 5000);
}

function loadbanners()
{
if (banners==0)
{
image=&quot;001.jpg&quot;;
}
if (banners==1)
{
image=&quot;002.jpg&quot;;
}
if (banners==2)
{
image=&quot;003.jpg&quot;;
}
if (banners==3)
{
image=&quot;004.jpg&quot;;
}
}
//-->
</script>

<BODY onLoad=&quot;window.setTimeout('cycle();',7000);(loadbanners());&quot;>
<IMG border=&quot;0&quot; src=&quot;001.jpg&quot; name=&quot;banner1&quot;>
 
Still, I'm curious - how to convert integer to string?
b2 - benbiddington@surf4nix.com
 
bangers, javascript is veeeeeeeeery cool on types, so your &quot;top&quot;+i would have worked fine - you'd get a STRING ! no need to convert an integer to a string then :)
 
Hmmm - try it out, it don'y work, seriously!
b2 - benbiddington@surf4nix.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top