Show random image using Javascript


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

Usage example:
randomImage(['0.gif',50,50,'1.gif',25,25,'2.gif',50,25]);


Copy this code and paste it in your HTML
  1. function randomImage(imgArr) {
  2. var imgSrc, imgW, imgH, r;
  3. r = Math.floor(Math.random()*(imgArr.length / 3))-1;
  4.  
  5. imgSrc = imgArr[r * 3];
  6. imgW = imgArr[(r * 3)+1];
  7. imgH = imgArr[(r * 3)+2];
  8.  
  9. document.write('<IMG SRC='+imgSrc+' WIDTH='+imgW+' HEIGHT='+imgH+'>');
  10. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.