Need JavaScript to randomly display link and two images...

Discussion in 'JavaScript' started by ccano, Oct 30, 2006.

  1. #1
    I need a JavaScript that will randomly display a link with two images, but the link is related to the two images, so they must appear together. For example:

    Text link: "Car Goes 300 MPH" w/ embedded link to buy car.

    [Image of car]

    Button w/ embedded link to buy car.

    See how the link and the images go together? I don't want the Script to randomly generate the links/images independent of each other.

    Is there a script that can do this? Thanks in advance :)
     
    ccano, Oct 30, 2006 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    Untested but should work.
    
    <script type="text/javascript">
    <!--
    var text1 = new Array();
    var text2 = new Array();
    var images = new Array();
    
    images[0] = 'http://www.url/image.jpg';
    text1[0] = 'Car Goes 300 MPH';
    text2[0] = 'Button ';
    
    images[1] = 'http://www.url/image.jpg';
    text1[1] = 'Car Goes 300 MPH';
    text2[1] = 'Button ';
    
    images[2] = 'http://www.url/image.jpg';
    text1[2] = 'Car Goes 300 MPH';
    text2[2] = 'Button ';
    
    var rand = Math.floor(Math.random()*images.length);
    
    document.write('<p>'+ text1[rand] +'</p>');
    document.write('<p>'+ images[rand] +'</p>');
    document.write('<p>'+ text2[rand] +'</p>');
    //-->
    </script>
    
    Code (markup):
     
    nico_swd, Nov 1, 2006 IP
  3. fadetoblack

    fadetoblack Peon

    Messages:
    416
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3

    what does this line of code signify?
     
    fadetoblack, Nov 13, 2006 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    Math.floor = Rounds a fractions down
    Math.random() = Returns a random number between 0 and 1
    *images.length); Multiplies this number by the length of the array.

    So all in all, it creates a random key which exists in the array so we can show a random image.
     
    nico_swd, Nov 14, 2006 IP
  5. fadetoblack

    fadetoblack Peon

    Messages:
    416
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    thnx .. that helped
     
    fadetoblack, Nov 14, 2006 IP