PDA

View Full Version : Random Images



adam123
10-07-2006, 12:55 PM
Hi I'm very new to javascript and would like some help.
I'm after a set of code that will display x amount of my images randomly. This code I have and it is working fine. But I would like so that if someone clicks image one.jpg it opens one.htm in target=window.

I have found a site that already has the code, I just cant figure out how to organise and set it up. Here is the site:http://www.vicsjavascripts.org.uk/GroupofRandomImages/GroupofRandomImages.htm

and here is the code I have so far (without the links)


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>
<title></title>
<script language="JavaScript" type="text/javascript">
<!--
Array.prototype.randomise=function(zxca){
zxccnt=0; zxcta=[];
while (zxccnt<zxca.length){
zxctmp=Math.floor(Math.random()*zxca.length-1)+1;
if (!zxcta[zxctmp]){zxcta[zxctmp]=zxca[zxccnt]; zxccnt++; }
}
return zxcta;
}

var ImgPath='http://www.vicsjavascripts.org.uk/StdImages/';
var ImgAry=new Array('Zero.gif','One.gif','Two.gif','Three.gif','Four.gif','Five.gif');

function Display(id){
var ary=[];
ary=ImgAry.randomise(ImgAry);
var imgs=document.getElementById(id).getElementsByTagName('IMG');
for (var zxc0=0;zxc0<imgs.length;zxc0++){
imgs[zxc0].src=ImgPath+ary[zxc0];
}
}
//-->
</script>

</head>

<body onload="Display('fred');" >
Will work for as many image tags in fred so long as ImgAry has enough images<br>
<a id="fred" >
<img width=100 height=100 >
<img width=100 height=100 >
<img width=100 height=100 >
<img width=100 height=100 >
</a>
</body>

</html>