Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    May 2003
    Location
    Provo Utah
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Random Images with Text for Each Image

    Code:
     <td height="400" colspan=2 background=" Change This Image ">
              <table width="688" height="400" border="0" cellpadding="0" cellspacing="0">
                <tr>
                  <td height="300" colspan="6" align="top">
                    <table width="100%" border="0" cellspacing="0" cellpadding="0">
                      <tr>
                        <td valign="top"> Text based on image Goes here </td>
                      </tr>
                    </table>
                  </td>
                </tr>


    I have in the past used to change my images using this script.

    Code:
    <SCRIPT LANGUAGE="Javascript">
    function image() {
    };
    
    image = new image();
    number = 0;
    
    // imageArray
    image[number++] = "<td height='400' colspan=2 background='1.jpg' border='0' height='249' width='491'>"
    image[number++] = "<td height='400' colspan=2 background='2.jpg' border='0' height='249' width='491'>"
    image[number++] = "<td height='400' colspan=2 background='3.jpg' border='0' height='249' width='491'>"
    increment = Math.floor(Math.random() * number);
    
    document.write(image[increment]);
    </sript>
    I am trying to figure out how to change text with the images. However the text isn't right next to the image but in a table cell.
    I tried if else statements and failed. Any Suggestions on how to accomplish this.

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Code:
    <script type="text/javascript">
    function setImage(){
       var arrImg = new Array();
       arrImg[0] = new Object();
       arrImg[0].src = "image1.gif";
       arrImg[0].text = "This is image 1";
    
       arrImg[1] = new Object();
       arrImg[1].src = "image2.gif";
       arrImg[1].text = "This is image 2";
    
       arrImg[2] = new Object();
       arrImg[2].src = "image3.gif";
       arrImg[2].text = "This is image 3";
      
       //...and so on
     
       //randomize
       var idx = Math.floor(Math.random()*arrImg.length);
    
       document.getElementById('colImage').style.backgroundImage='url(' + arrImg[idx].src + ')';
       document.getElementById('colText').innerHTML=arrImg[idx].text;
    } 
    </script>
    ...
    <body onload="setImage()">
    ...
     <td id="colImage" colspan="2" style="height:400px;background-image:url(default.gif)">
         ...
                <td valign="top" id="colText">Text for the default image background</td>
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #3
    New Coder
    Join Date
    May 2003
    Location
    Provo Utah
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I thought I would post what I did with this code.

    Check it out at http://www.sundanceresort.com/test.html

  • #4
    Regular Coder
    Join Date
    Aug 2002
    Location
    USA
    Posts
    625
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi colorspots:

    I would only further suggest that you reference the images sequentially...with such a small collection. Only if you had 10 or 20 images, would random display be practical.



    -james


    Thanks, Glenn.
    "God so loved the world that he gave his only begotten son, so that whosoever believed in him would not perish, but have everlasting life. For God did not send his son into the world to condemn the world, but so that through him the world might be saved. "

  • #5
    New Coder
    Join Date
    May 2003
    Location
    Provo Utah
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have considered that. I am planning on using 10-20 different images and messages. I feel the sequential the last images/message will get less exposure so I thought randomizing it allowed more variation of viewing.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •