Solved

multiple random images

Posted on 1997-01-24
1
311 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

"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

809 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