faqts : Computers : Programming : Languages : JavaScript : Images

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

19 of 38 people (50%) answered Yes
Recently 3 of 10 people (30%) answered Yes

Entry

How do I display a random image every time the page loads?

Apr 30th, 2000 06:10
Martin Honnen,


Well you make an array of img urls e.g.
  var images = new Array (
    'whatever.gif',
    'whatelse.gif',
    'whateverelse.gif'
  );
and then use
http://www.faqts.com/knowledge-base/view.phtml/aid/889/fid/142
to get a random index into that array:
  var randomIndex = Math.floor(images.length * Math.random());
and then you document.write the IMG tag
  document.write('<IMG SRC="' + images[randomIndex] + '">');