dcsimg
www.webdeveloper.com
Results 1 to 7 of 7

Thread: random image background

  1. #1
    Join Date
    Feb 2003
    Location
    IL
    Posts
    104

    Cool random image background

    Ok hopefully i won't need any help from you guys abter this. Right now i have a background image on my site and it is of a member of the band my site is about. What i want is to have a 1 of 4 pics randomly come up when the page is entered or refreshed. I have seen this before so i know it can be done. I just can't find it on htmlgoodies.com or echoecho.com. Hopefully someone has done this and knows how to do it. The only thing i have found is a random background color.

  2. #2
    Join Date
    Jan 2003
    Location
    Jersey Girl
    Posts
    1,198
    Hi George (again!)...

    The site looks a lot better since the last time I viewed it (no more whaky text when using Netscape. yay!).

    To answer your question, here is a background image changer from Javascript.com:

    http://javascript.internet.com/bgeffects/r-images.html

    I'm not sure what this would do if a visitor has Javascript disabled (since the <body> is written client-side).

    Also, it seems that the random number process could be a bit simpler (but that is definitely not my area of expertise). I'm just thinking that maybe it could be simplified (if someone - not me! - knew how to do such things. lol)

    Hope this helps... and I'm glad that things are progressing for you.

    rock on...
    k

  3. #3
    Join Date
    Dec 2002
    Location
    Pleasanton, CA
    Posts
    2,132
    Try this...
    <html><head><title>Untitled</title>
    <script type="text/javascript">
    <!--
    var imgAry = new Array('sample1.jpg','sample2.jpg','sample3.jpg','sample4.jpg')
    var randomNumber = Math.floor(Math.random()*3); // creates a ramdom number between 1 and 4 (0 and 3)
    document.open();
    document.write("<body background=\"" +imgAry[randomNumber]+ "\">");
    //-->
    </script>
    </head>

    <body bgcolor="#ffffff">


    </body>
    </html>

    Update:
    I just noticed a small error. The random number should be 0 to 3
    Last edited by Nedals; 03-23-2003 at 01:28 PM.

  4. #4
    Join Date
    Jan 2003
    Location
    Jersey Girl
    Posts
    1,198
    WOW Nedals...
    That's SWEEEEEET!!!

    (it's strange - well.... maybe not - but it seems that so many of the scripts from Javascript.com are really just too bloated and complex. No wonder I can't learn from that stuff!)

    I understand this one though!
    Thanks Nedals!
    k

  5. #5
    Join Date
    Feb 2003
    Location
    IL
    Posts
    104
    Hmmmmmm I tried both of those and neither one worked. The link that Khaki gave me i didn't really like cause you have to hit a button for the image to change. And the script that Nedals posted didn't work either. I don't know why it didn't cause i could somewhat understand what would do what. But thanks for trying to help. I'll be patient and mabey someone will find something.

  6. #6
    Join Date
    Jan 2003
    Location
    Jersey Girl
    Posts
    1,198
    Hi George...

    I can understand not liking the linked script (it only uses the button for the example y'know... the script actually works onLoad or refresh).

    Nedals version works great! You must be doing something wrong.

    Post how you tried to use it...
    (did you alter the * 3 part to match the number of images in your array? Remember... 0 counts too)

    confused...
    k

  7. #7
    Join Date
    Feb 2003
    Location
    IL
    Posts
    104
    Nevermind i just got it to work like 5 seconds ago. Thanks again you guys. Now i just need to get my links up on everpage.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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



Recent Articles