JavaScript Random Image Display

By Mash B

Displaying a Random Image on Browser upon reloading page using Javascript. Although there is always a possible chance the same random number will be selected twice in a row.

<script language=”JavaScript”>
     // Assign image paths to array object
var imageList = new Array;
imageList[0] = “image1.jpg”;
imageList[1] = “image2.jpg”;
imageList[2] = “image3.jpg”;
imageList[3] = “image4.jpg”;

// get the random number
var imageChoice = Math.floor(Math.random() * imageList.length);

// display random image
document.write(‘<img src=”’ + imageList[imageChoice] + ‘“>’);

</script>

JavaScript Random Image Display  (820 Views)