Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Random SET of images

Status
Not open for further replies.

artdirectoreric

Instructor
Jun 26, 2001
120
0
0
US
I have a site that the client wants to change the background image in the upper left randomly. This image needs to be broken up for reasons I won't get into. I've found tutorials on how to make a random image, but not on how to make a random SET of images. I've created 4 folders of images and have named them 1, 2, 3 and 4. I want a random number generated when a visitors comes to the site and I want the img src tags to use the same number that was generated to find all of the images for a certain section of the page. So, if the function returned a 3, I want say, six different img src tags to use the images in folder 3.
 

i assume the pictures won't change once the page is loaded, but will be different on each page load. right?

post the code of the random generator...

- g
 
Well, I might as well not bother. I just tried a few different scripts from sites I found and tried to alter them to do what I wanted them to do. What's left on my test page is just gibberish.
 
Basically, here's what it needs to do.

1) Generate a random number from 1 to 7 once each time the page is loaded

2) Place that number in a variable

3) Allow me to place that variable in several image tags without generating a new random number.
 
I figured it out, or at least found a way to do it.

Code:
<SCRIPT LANGUAGE=&quot;JavaScript&quot;><!--
var now = new Date();
var secondscheck = now.getSeconds() + 1;
var imagefolder=0
if (secondscheck <= 15 && secondscheck >=1){
                imagefolder=1;
        }
if (secondscheck <= 30 && secondscheck >=16){
                imagefolder=2;
        }
if (secondscheck <= 45 && secondscheck >=31){
                imagefolder=3;
        }
if (secondscheck <= 60 && secondscheck >=46){
                imagefolder=4;
        }
//--></SCRIPT>

And then I used the imagefolder variable in the image tags like this:

Code:
<script language=&quot;JavaScript&quot;><!--
document.write('<img src=&quot;URL/' + imagefolder + '/images/Untitled-1_01.gif&quot; width=&quot;126&quot; height=&quot;139&quot;>');
//--></script>

Voila!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top