Log in

View Full Version : Random picture selection with links to different pages



Yabzab
06-09-2011, 12:45 PM
I want to try and create a script that will:
a) Randomly insert a different .gif file
b) With a link to another page specific to that .gif image.
c) The .gif and link will change everytime the webpage is loaded/refreshed.

I am thinking probably javascript is the easiest way to do this by using the VAR in Java, but I have no idea how to actually achieve this. Please can you suggest something.

Thanks

Yabzab

vwphillips
06-09-2011, 02:46 PM
<!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></title>
</head>

<body>
<a id="tst" href="#"><img src="http://www.vicsjavascripts.org.uk/StdImages/Blank.gif" width="100" height="100" /></a>

<script type="text/javascript">
/*<![CDATA[*/

function Swap(id,ary){
var a=document.getElementById(id);
ary=ary[Math.floor(Math.random()*ary.length)];
a.href=ary[1];
a.getElementsByTagName('IMG')[0].src=ary[0];
}

var Ary=[
['http://www.vicsjavascripts.org.uk/StdImages/One.gif','http://www.vicsjavascripts.org.uk/'],
['http://www.vicsjavascripts.org.uk/StdImages/Two.gif','http://www.dynamicdrive.com/'],
['http://www.vicsjavascripts.org.uk/StdImages/Three.gif','http://www.codingforums.com/']
];

Swap('tst',Ary);
/*]]>*/
</script>
</body>

</html>