Forum Moderators: open
function random_imglink(){
var myimages=new Array()
//random images
myimages[1]="img1.gif"
myimages[2]="img2.gif"
myimages[3]="img3.gif"
myimages[4]="img4.gif"
myimages[5]="img5.gif"
myimages[6]="img6.gif"
myimages[7]="img7.gif"
myimages[8]="img8.gif"
myimages[9]="img9.gif"
myimages[10]="img10.gif"
myimages[11]="img11.gif"
//corresponding links
var imagelinks=new Array()
imagelinks[1]="http://www.url.com/page1.php"
imagelinks[2]="http://www.url.com/page2.php"
imagelinks[3]="http://www.url.com/page3.php"
imagelinks[4]="http://www.url.com/page4.php"
imagelinks[5]="http://www.url.com/page5.php"
imagelinks[6]="http://www.url.com/page6.php"
imagelinks[7]="http://www.url.com/page7.php"
imagelinks[8]="http://www.url.com/page8.php"
imagelinks[9]="http://www.url.com/page9.php"
imagelinks[10]="http://www.url.com/page10.php"
imagelinks[11]="http://www.url.com/page11.php"
var ry=Math.floor(Math.random()*myimages.length)
if (ry==0)
ry=1
document.write('<a href='+'"'+imagelinks[ry]+'"'+'><img src="'+myimages[ry]+'" border=0></a>')
}
random_imglink()
//-->
</script> But now I want 7 images displayed, instead of just one, all collecting links and pics out of the same array. The links should still be corresponding to the image.
The other problem is that there are 11 links, and I only want to show 7.
So I tried this:
if (ry==0)
ry=7
document.write('<a href='+'"'+imagelinks[ry]+'"'+'><img src="'+myimages[ry]+'" border=0></a>',
'<a href='+'"'+imagelinks[ry]+'"'+'><img src="'+myimages[ry]+'" border=0></a>',
'<a href='+'"'+imagelinks[ry]+'"'+'><img src="'+myimages[ry]+'" border=0></a>',
'<a href='+'"'+imagelinks[ry]+'"'+'><img src="'+myimages[ry]+'" border=0></a>',
'<a href='+'"'+imagelinks[ry]+'"'+'><img src="'+myimages[ry]+'" border=0></a>',
'<a href='+'"'+imagelinks[ry]+'"'+'><img src="'+myimages[ry]+'" border=0></a>',
'<a href='+'"'+imagelinks[ry]+'"'+'><img src="'+myimages[ry]+'" border=0></a>')
} But this doesnt work..
Can someone guide me into the right direction?
try it like this...
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript"><title></title>
<style type="text/css">
img {
border:0;
}
</style><script type="text/javascript">
var linksNumber=7; [blue]/* This is the number of links to be shown and matches the HTML */[/blue]
var myimages=[
'img1.gif','img2.gif',
'img3.gif','img4.gif',
'img5.gif','img6.gif',
'img7.gif','img8.gif',
'img9.gif','img10.gif',
'img11.gif'
];var imagelinks=[
'page1.php','page2.php',
'page3.php','page4.php',
'page5.php','page6.php',
'page7.php','page8.php',
'page9.php','page10.php',
'page11.php'
]var ary0=[];
var ary1=[];
var k=0
var c,n;function randomImgDisplay() {
for(c=0;c<myimages.length;c++) {
ary0[c]=c; [blue]/* Pre-populate 'ary0' with numbers 0 to 11 */[/blue]
}
while(ary0.length>0) {n=Math.floor(Math.random()*ary0.length); [blue]/* Get a random 'ary0' index number */[/blue]
ary1[k]=ary0[n]; [blue]/* Add 'ary0' value to 'ary1' */[/blue]
k++;
ary0.splice(n,1); [blue]/* Remove that element from the 'ary0'. The array gets shorter with each iteration */[/blue]}
for(c=0;c<linksNumber;c++){
document.getElementById('pic'+c).src=myimages[ary1[c]]; [blue] /* replace the image positions */[/blue]
document.getElementById('lnk'+c).href=imagelinks[ary1[c]]; [blue]/* replace the link positions */[/blue]document.getElementById('pic'+c).alt=myimages[ary1[c]]; [red]/* this is for testing, and maybe removed */[/red]
}
}
if(window.addEventListener){
window.addEventListener('load',randomImgDisplay,false);
}
else {
if(window.attachEvent){
window.attachEvent('onload',randomImgDisplay);
}
}</script>
</head>
<body><div>
<a id="lnk0" href="page1.php"><img id="pic0" src="img1.gif" alt=""></a>
<a id="lnk1" href="page2.php"><img id="pic1" src="img2.gif" alt=""></a>
<a id="lnk2" href="page3.php"><img id="pic2" src="img3.gif" alt=""></a>
<a id="lnk3" href="page4.php"><img id="pic3" src="img4.gif" alt=""></a>
<a id="lnk4" href="page5.php"><img id="pic4" src="img5.gif" alt=""></a>
<a id="lnk5" href="page6.php"><img id="pic5" src="img6.gif" alt=""></a>
<a id="lnk6" href="page7.php"><img id="pic6" src="img7.gif" alt=""></a>
</div></body>
</html>