Solved

multiple random images

Posted on 1997-01-24
1
321 Views
Last Modified: 2013-12-25
can i have several images strung to a page so that every time the page is accessed it will load a different image randomly? As in if someone visited my page several times they would see a different logo each time.
0
Comment
Question by:D.Murray
1 Comment
 

Accepted Solution

by:
DocOzone earned 200 total points
ID: 1865377
Hi there D.Murray! Sure, you can do this using Javascript, but it will only work with the newest versions of Netscape (3.0 and 4.0). Presumably the next version of Internet Explorer will handle the "image" object in Jscript. Here's what you do...
   Inside your <head> tags, include this script...
-------
<SCRIPT LANGUAGE="JavaScript">
<!--I start and end every javascript section with traditional comments, just in case.

// First, avoid any browser but NS3, we'll use this statement at the start of every function
        if (navigator.userAgent.substring(0,9) == "Mozilla/3") {
// Now make the array called myimages
        myimages = new Array(4);
    for (m=0; m<=3; m++) {
    myimages[m] = new Image(); }
        myimages[0].src = "images/myimage1.jpeg";
        myimages[1].src = "images/myimage2.jpeg";
        myimages[2].src = "images/myimage3.jpeg";
        myimages[3].src = "images/myimage4.jpeg";  }
// End the array creation
 
// Here's a random image generator function.
       function randoscript() {
                if (navigator.userAgent.substring(0,9) == "Mozilla/3") {
                rando = Math.round( Math.random() * 3 );
                document.myimage.src = myimages[rando].src; }}
// This is the end of the random function.

// end scripts   -->
</SCRIPT>
-----------
Replace the calls for "myimageXXX.jpeg " with the path to your images. Note that all of these images should be the same size, or they will be re-sized to fit in the space of the first image. Now, in your HTML, place the first image, where our random image will go. You can fill this space with a blank dummy file if you'd like, but I'll do it with one of the files from the "myimage" series. Reference it like this...
---------
<A HREF="javascript: randoscript()"><img name="myimage" src="images/myimage1.jpeg"></A>
---------
I've put this inside an <a href> link so you can click on the image to load a new random image. You want it to pick a random image as it loads, so in your <body> tag, include this...
---------
<body onLoad="randoscript()">
---------
That's it. You can see this script in action at "http://www.ozones.com/~drozone/java-templates/index6.html"

Take care! Your pal,   -doc-

0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Read about why website design really matters in today's demanding market.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
The viewer will learn how to count occurrences of each item in an array.

831 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