Posted By

a1ias on 07/28/11


Tagged

random


Versions (?)

Show random image using Javascript


 / Published in: JavaScript
 

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

  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  

You need to login to post a comment.