Displaying A Random Image

/ Published in: JavaScript
Save to your folder(s)

A nice little code snippet for displaying images randomly when a page is reloaded.

Copy this code and paste it in your HTML
  1. window.onload = choosePic;
  3. var myPix = new Array("images/00.jpg","images/01.jpg","images/02.jpg","images/03.jpg","images/04.jpg","images/05.jpg","images/06.jpg","images/07.jpg");
  5. function choosePic() {
  6. randomNum = Math.floor((Math.random() * myPix.length));
  7. document.getElementById("myPicture").src = myPix[randomNum];
  8. }

Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.