SitePoint Sponsor

User Tag List

Results 1 to 20 of 20

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2003
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    JS Random Table Background Image Rotator script wanted.

    Hi All,
    Has anybody an idea where I can get a free JS Random Background Image Rotator script? What I really want is, any time the site is visited or the refresh button is hit, a new background image should appear.

    I have been searching the whole web for this script for the past two weeks with no success. I shall be very glad if somebody will help me out.

    Thanks in advance.

    CocoX.
    Last edited by CocoX; Oct 29, 2003 at 05:46.
    P E A C E IS THE ONLY WAY TO P E A C E ...!

  2. #2
    SitePoint Member
    Join Date
    Oct 2003
    Location
    USA
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Place this script segment after the opening body tag.
    Code:
    <script type="text/javascript">
    var bg_list = new Array("bkggif.gif", "marble.gif", "sidebar.gif");
    var rand_bg = bg_list[ Math.floor( Math.random() * bg_list.length ) ]; 
    if (document.getElementsByTagName)
      document.getElementsByTagName('body')[0].style.backgroundImage = "url("+rand_bg+")";
    </script>

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2003
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Uneverno,
    Thanks so much for the script but I still have some questions.
    Where exactly do you want me to put the script?
    Can I use it as a Table-background?

    I guess I have to explain further where exactly I want the image to appear. What I really want is, I don't want the background image to appear in the whole page but rather a particular Table as a TABLE BACKGROUND.
    e.g.
    <table border="0" width="100%" align="center" style="background-image:url('../images/bg_image001.jpg'); background-repeat:no-repeat;">


    Is that really possible? I have been seeing this function in a lot of websites.

    Thanks again in advance.

    CocoX.
    P E A C E IS THE ONLY WAY TO P E A C E ...!

  4. #4
    SitePoint Member
    Join Date
    Oct 2003
    Location
    USA
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you want to apply it to a table rather than the page, change the last two lines of the code to:

    Code:
    if (document.getElementById)
      document.getElementById('tableID').style.backgroundImage = "url("+rand_bg+")";
    Place the script segment after the table.

  5. #5
    SitePoint Enthusiast
    Join Date
    Feb 2003
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Uneverno,
    Thanks again but I could not get the image appear in the TABLE. I am getting Object Requird error.

    Here is what I did and I am not really sure whether I am missing something:

    <html>
    <head>
    <title>No title</title>
    </head>
    <body bgcolor="white" text="black" link="blue" vlink="purple" alink="red">
    <table border="1">
    <script type="text/javascript">
    var bg_list = new Array("bkggif.gif", "marble.gif", "sidebar.gif" ); var rand_bg = bg_list[ Math.floor( Math.random() * bg_list.length ) ]; if (document.getElementById) document.getElementById('tableID').style.backgroundImage = "url("+rand_bg+" )";
    </script>
    <tr>
    <td width="947" height="452">
    </td>
    </tr>
    </table>
    </body>
    </html>
    P E A C E IS THE ONLY WAY TO P E A C E ...!

  6. #6
    SitePoint Enthusiast
    Join Date
    Apr 2003
    Location
    Maryland, USA
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi CocoX,
    The reason this is not working is because JavaScript can not find the ID 'tableID'. To fix this, add an ID to your table tag

    Code:
    <table border="1" ID='tableID'>

  7. #7
    SitePoint Enthusiast
    Join Date
    Feb 2003
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by danielng5
    Hi CocoX,
    The reason this is not working is because JavaScript can not find the ID 'tableID'. To fix this, add an ID to your table tag

    Code:
    <table border="1" ID='tableID'>
    Hi Danielng5,
    Thanks so much for the tip.

    Sorry I have added the TableID but nothing happen.

    The error is still there: OBJECT REQUIRED.

    Thanks again.

    CocoX.
    P E A C E IS THE ONLY WAY TO P E A C E ...!

  8. #8
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    CocoX:

    The script you're using there isn't really 'random' at all - because, every time the user refreshes the page, another 'random' background is chosen, and it could easily be the same one, with the images repeating irregularly, over and over. Did you want a different background every time the page is loaded, cycling through a group of images in a fixed order? The 'fixed' order would be randomized, of course, every time someone came to your site.
    ::: certified wild guess :::

  9. #9
    SitePoint Enthusiast
    Join Date
    Feb 2003
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by adios
    CocoX:

    The script you're using there isn't really 'random' at all - because, every time the user refreshes the page, another 'random' background is chosen, and it could easily be the same one, with the images repeating irregularly, over and over. Did you want a different background every time the page is loaded, cycling through a group of images in a fixed order? The 'fixed' order would be randomized, of course, every time someone came to your site.
    Yea Adios that's exactly what I am looking for. Please could you help me out.

    Thanks in advance.

    CocoX.
    P E A C E IS THE ONLY WAY TO P E A C E ...!

  10. #10
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sure...give me a sec' (eating lunch!).

    Post your image urls - if they're online (or zip them up and attach them) - and anything else you think would help.
    ::: certified wild guess :::


Bookmarks

Posting Permissions

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