Home : Internet : JavaScript : Random Image with Link

How to Display a Random Image With Hyperlink

This Javascript is a variation on the random image code that adds the option to make each image a hyperlink. This is a simplified version—note that each image's full path must be included in the image attributes.

<script type="text/javascript">
var total_images = 3;
var random_number = Math.floor((Math.random()*total_images));
var random_img = new Array();
random_img[0] = '<a href="page1.html"><img src="images/image1.gif"></a>';
random_img[1] = '<a href="page2.html"><img src="images/image2.gif"></a>';
random_img[2] = '<a href="page3.html"><img src="images/image3.gif"></a>';
document.write(random_img[random_number]);
</script>

Note: You can have any number of random images—just make sure you change the "total_images" variable to reflect the total number.