Please help - I need to display a random image every time the page is refreshed

Discussion in 'JavaScript' started by irideflatland, Jul 21, 2006.

  1. #1
    I have been trying PHP and it didn't work, so I changed the htaccess file and then I f'd up my server, so is there a way to do this with JavaScript?

    Green reps if you help me :D
     
    irideflatland, Jul 21, 2006 IP
  2. irideflatland

    irideflatland Banned

    Messages:
    515
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Ok, after Googling and looking at a few sites, I found this -

    <script language="JavaScript"><!--
    
    
    images = new Array(4);
    
    images[0] = "<img src='http://www.fpsgaming.us/images/halo_3.jpg'>";
    
    images[1] = "<img src='http://www.computerhope.com/banners/banner2.gif'>";
    
    images[2] = "<a href = 'http://www.computerhope.com/index.htm'><img 
    
    src='http://www.computerhope.com/banners/banner3.gif' alt='Visit Computer Hope'></a>";
    
    images[3] = "<a href = 'http://www.computerhope.com/newslet.htm'><img 
    
    src='http://www.computerhope.com/banners/banner4.gif' alt='Computer Hope Newsletter'></a>";
    
    index = Math.floor(Math.random() * images.length);
    
    document.write(images[index]);
    
    //done
    
    // --></script>
    Code (markup):
    So that's what I'm going to use.
     
    irideflatland, Jul 21, 2006 IP
  3. ccoonen

    ccoonen Well-Known Member

    Messages:
    1,606
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    160
    #3
    i would still do it server-side though. What happens when you have 1000 images to randomize? Then you have to load 1000 lines of HTML just to view one image.

    that code above converted to php is extremely similar...
     
    ccoonen, Jul 24, 2006 IP
  4. irideflatland

    irideflatland Banned

    Messages:
    515
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Well PHP doesn't get along with me. And I'm not going to ever have more than 10 images. Most likely I'll just stick with my 4.
     
    irideflatland, Jul 25, 2006 IP