Random Image on Page Javascript

This little bit of code will allow you to revolve several images on a web page each time the page reloads. Just update your image location between the "" for each myimages variable and the corresponding link between the "" for each imagelinks variable. Add more if you like or take some away.

  1.  
  2. <script language="JavaScript">
  3. <!--
  4.  
  5. function random_imglink(){
  6.  
  7. //specify random images below. You can have as many as you wish
  8. var myimages=new Array()
  9. myimages[1]="images/image.png"
  10. myimages[2]="images/image.png"
  11. myimages[3]="images/image.png"
  12. myimages[4]="images/image.png"
  13. myimages[5]="images/image.png"
  14.  
  15. //specify corresponding links below
  16. var imagelinks=new Array()
  17. imagelinks[1]="http://website.com/index.php"
  18. imagelinks[2]="http://website.com/index.php"
  19. imagelinks[3]="http://website.com/index.php"
  20. imagelinks[4]="http://website.com/index.php"
  21. imagelinks[5]="http://website.com/index.php"
  22.  
  23. var ry=Math.floor(Math.random()*myimages.length)
  24.  
  25. if (ry==0)
  26. ry=1
  27. document.write('<a href='+'"'+imagelinks[ry]+'"'+'><img src="'+myimages[ry]+'" border=0></a>')
  28. }
  29.  
  30. random_imglink()
  31. //-->
  32. </script>
  33.  

Leave a Reply

You can use these XHTML tags: <a href="" title=""> <abbr title=""> <acronym title=""> <blockquote cite=""> <code> <em> <strong>