Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2006-07-07
3
203 Views
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.
0
Comment
Question by:ml1n4
  • 2
3 Comments
 
LVL 1

Expert Comment

by:sushestvo
ID: 17061060
<html>
 <body bgcolor="#EDEDED" text="#000000" link="#000000">
 <center>

<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='http://www.womir.com/'><img src='004.jpg' align=center hspace=10></a>"); return;
 }
   if (x > .66)
 {document.write("<A HREF='http://www.womir.com/'><img src='005.jpg' align=center hspace=10></a>"); return;
 }
   if (x > .55)
 {document.write("<A HREF='http://www.womir.com/'><img src='006.jpg' align=center hspace=10></a>"); return;
 }
   if (x > .44)
 {document.write("<A HREF='http://www.womir.com/'><img src='007.jpg' align=center hspace=10></a>"); return;
 }
   if (x > .33)
 {document.write("<A HREF='http://www.womir.com/'><img src='008.jpg' align=center hspace=10></a>"); return;
 }
   if (x > .22)
 {document.write("<A HREF='http://www.womir.com/'><img src='005.jpg' align=center hspace=10></a>"); return;
 }
   if (x > .11)
 {document.write("<A HREF='http://www.womir.com/'><img src='004.jpg' align=center hspace=10></a>"); return;
 }
   if (x > 0)  
 {document.write("<A HREF='http://www.womir.com/'><img src='008.jpg' align=center hspace=10></a>"); return;
 }
 }
 
 RandomGraphics();
 </SCRIPT>
<b>Reload page.</b>
 </body>
 </html>
0
 
LVL 5

Accepted Solution

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

Expert Comment

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

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

792 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