picking random images


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: picking random images

  1. #1
    Join Date
    May 2006
    Posts
    2

    picking random images

    Hi,

    I'm trying, on a website, to have inserted in 2 separate cells of a table 2 packs of cards. The idea is that when the page loads pack 1 shows 'club5.gif' then randomly changes when clicked upon. Whilst pack 2 shows 'card0.gif' then also randomly changes when that is clicked upon.

    The included code at the moment works as follows: On loading the page pack1 has a random starting choice but after that works fine. Pack 2 has no image to start with but upon clicking on the 'no image sign' works fine.

    How to I determine the start image for each function


    <head>
    <SCRIPT LANGUAGE="JavaScript">

    var rand1 = 0;
    var useRand = 0;
    var rand12 = 0;
    var useRand2 = 0;

    images = new Array;
    images[1] = new Image();
    images[1].src = "club5.gif";
    images[2] = new Image();
    images[2].src = "club6.gif";
    images[3] = new Image();
    images[3].src = "club7.gif";

    images1 = new Array;
    images1[1] = new Image();
    images1[1].src = "card0.gif";
    images1[2] = new Image();
    images1[2].src = "card1.gif";
    images1[3] = new Image();
    images1[3].src = "card2.gif";

    function swapPic() {
    var imgnum = images.length - 1;
    do {
    var randnum = Math.random();
    rand1 = Math.round((imgnum - 1) * randnum) + 1;
    } while (rand1 == useRand);
    useRand = rand1;
    document.randimg.src = images[useRand].src;
    }

    function swap1Pic() {
    var imgnum1 = images1.length - 1;
    do {
    var randnum1 = Math.random();
    rand12 = Math.round((imgnum1 - 1) * randnum1) + 1;
    } while (rand12 == useRand2);
    useRand2 = rand12;
    document.randimg1.src = images1[useRand2].src;
    }

    </script>
    </head>



    <body OnLoad="swapPic()">

    then in one cell of my table....

    <a onClick="swapPic();"><img name="randimg" src="image.gif"></a><br>


    and in another cell....

    <a onClick="swap1Pic();"><img name="randimg1" src="image.gif"></a><br>

  2. #2
    Join Date
    Mar 2006
    Posts
    37

    Program

    You might have better luck if you post this in the web forum. This is for the java programming language.

Similar Threads

  1. Replies: 0
    Last Post: 10-29-2001, 02:55 PM
  2. loading images dynamically is slow
    By Michael Shutt in forum Web
    Replies: 0
    Last Post: 06-26-2001, 03:25 PM
  3. Re: We don't get round trips...
    By Avron Polakow in forum Web
    Replies: 1
    Last Post: 11-06-2000, 03:09 PM
  4. Caching images using VBScript
    By Larry Serflaten in forum Web
    Replies: 2
    Last Post: 11-01-2000, 01:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center