Random images on page load - Both Javascript & html required...

Posted on 2006-07-07
Last Modified: 2010-04-06
Hello, this is a question that's been asked many times before but in fragments due to people usually knowing how to get started...  Can we assume NO knowledge on my part as to how to do this? Okay, here goes:

How do I get images to display randomly on a page each time it reloads?
i.e. A page banner that is different each time it is visited.  (I don't want the image to change whilst actually on screen).

I know I need a combination of Javascript and HTML but that's all I know.  Could somebody supply all the necessary code and end this question forever?

Many thanks indeed.
Question by:ml1n4
  • 2

Expert Comment

ID: 17061060
 <body bgcolor="#EDEDED" text="#000000" link="#000000">

<Script Language ="JavaScript">
 function RandomNumber()
   var today = new Date();
   var num= Math.abs(Math.sin(today.getTime()/1000));
   return num;
  function RandomGraphics()
   var x = RandomNumber();
    if (x > .77)
 {document.write("<A HREF=''><img src='004.jpg' align=center hspace=10></a>"); return;
   if (x > .66)
 {document.write("<A HREF=''><img src='005.jpg' align=center hspace=10></a>"); return;
   if (x > .55)
 {document.write("<A HREF=''><img src='006.jpg' align=center hspace=10></a>"); return;
   if (x > .44)
 {document.write("<A HREF=''><img src='007.jpg' align=center hspace=10></a>"); return;
   if (x > .33)
 {document.write("<A HREF=''><img src='008.jpg' align=center hspace=10></a>"); return;
   if (x > .22)
 {document.write("<A HREF=''><img src='005.jpg' align=center hspace=10></a>"); return;
   if (x > .11)
 {document.write("<A HREF=''><img src='004.jpg' align=center hspace=10></a>"); return;
   if (x > 0)  
 {document.write("<A HREF=''><img src='008.jpg' align=center hspace=10></a>"); return;
<b>Reload page.</b>

Accepted Solution

koolie earned 500 total points
ID: 17061156
I think you'll find this link is exactly what you are looking for.
It is simple and to the point.  I was going to copy it out, but it will be easier for you from the link

Expert Comment

ID: 17061181
One other note about the link I posted, if you want to learn off of it it is very well documented

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now