How do you make it so that a random image loads each time(out of 5 or 6)

Discussion in 'HTML & Website Design' started by newbiesiteguy, Feb 6, 2006.

  1. #1
    I want to have a bunch of different ads, that I create, and have a random one of them load each time someone goes to that page. Is there a way to do this?
     
    newbiesiteguy, Feb 6, 2006 IP
  2. jazzylee77

    jazzylee77 Peon

    Messages:
    578
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Sure! here is one way to do it.
     
    jazzylee77, Feb 6, 2006 IP
  3. Blame Me

    Blame Me Guest

    Messages:
    162
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Try this also a small java script

    http://www.bannercreator.nu/banner-rotation.html
     
    Blame Me, Feb 7, 2006 IP
  4. the_pm

    the_pm Peon

    Messages:
    332
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    0
    #4
    If this is for banners, and you want to ensure fair rotation, here is an in-depth article for a script that will do this for you. It will appear random to the end user, but it will ensure even exposure of the random items: http://www.iwdn.net/showthread.php?t=3414

    If you're not concerned with even exposure, and variety is all that's needed, here's a VERY simple script and some discussion about it. I'm using it in one site to deliver a "Tip of the Day" type of thing. http://www.iwdn.net/showthread.php?t=3349

    The nice thing about these scripts is that they are very easy to use, and since they do not rely on JavaScript, you don't have visitors missing out on their contents, and search engines will pick up on them much better. Plus, you don't have to create <noscript> alternatives.
     
    the_pm, Feb 7, 2006 IP
    Colleen likes this.
  5. YIAM

    YIAM Notable Member

    Messages:
    2,480
    Likes Received:
    240
    Best Answers:
    0
    Trophy Points:
    280
    #5
    Here is a simple code that will display a randum banner each time page is loaded.

    part1 place it between <head> and </head>

    
    <script language="JavaScript">
    <!--
    
    // Set up the image files to be used.
    var theImages = new Array() // do not change this
    
    // To add more image files, continue with the
    // pattern below, adding to the array. Rememeber
    // to increment the theImages[x] index!
    
    theImages[0] = '100.jpg'
    theImages[1] = 'acc.jpg'
    theImages[2] = 'xyz.jpg'
    theImages[3] = '10s0.jpg'
    theImages[4] = 'ascc.jpg'
    theImages[5] = 'xyzs.jpg'
    
    // ======================================
    // do not change anything below this line
    // ======================================
    
    var j = 0
    var p = theImages.length;
    
    var preBuffer = new Array()
    for (i = 0; i < p; i++){
       preBuffer[i] = new Image()
       preBuffer[i].src = theImages[i]
    }
    
    var whichImage = Math.round(Math.random()*(p-1));
    function showImage(){
    document.write('<img src="'+theImages[whichImage]+'">');
    }
    
    //-->
    </script>
    
    Code (markup):
    part 2: place it between <body></body>tags at your desired place

    <script language="JavaScript">
    
    showImage();
    //-->
    </script>
    Code (markup):
    You can modify it as per your requirement.
     
    YIAM, Feb 9, 2006 IP
  6. newbiesiteguy

    newbiesiteguy Peon

    Messages:
    58
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6

    In part one, do you want to write out the entire path of the image - http://www.etc. ?
     
    newbiesiteguy, Feb 10, 2006 IP
  7. YIAM

    YIAM Notable Member

    Messages:
    2,480
    Likes Received:
    240
    Best Answers:
    0
    Trophy Points:
    280
    #7
    yes you should write it.

    Is the script working?
     
    YIAM, Feb 11, 2006 IP
  8. newbiesiteguy

    newbiesiteguy Peon

    Messages:
    58
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    yes its working for images, but not for html docs.

    Im pretty new to this, so maybe im doing somthing obviously wrong.

    Should it work for html docs?
     
    newbiesiteguy, Feb 11, 2006 IP
  9. Notting

    Notting Notable Member

    Messages:
    3,210
    Likes Received:
    335
    Best Answers:
    0
    Trophy Points:
    280
    #9
    Thanks for that - ill use this!
     
    Notting, Feb 12, 2006 IP
  10. studio606

    studio606 Peon

    Messages:
    110
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Thanks for the script, bcabank! Works well for me on a random image splash page I just designed.
     
    studio606, Feb 12, 2006 IP
  11. mariush

    mariush Peon

    Messages:
    562
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #11
    As he designed the code, no, it would work only for images.
     
    mariush, Feb 12, 2006 IP
  12. jazzylee77

    jazzylee77 Peon

    Messages:
    578
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #12
    The method I linked to can rotate any code. It randomly inserts from different lines of a .txt file. You may have to edit some of your code to make it appear on one line, but this is a simple matter of clicking "end" "delete" down end delete... unless you have a tool that removes extra white space. If even rotation is important, I really haven't looked at the issues pointed out by the_pm.
     
    jazzylee77, Feb 12, 2006 IP
  13. the_pm

    the_pm Peon

    Messages:
    332
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    0
    #13
    The biggest issue with all of the methods in use here, with the exception of those jazzylee77 and I posed, is that they rely on the client to produce results. This means someone with JavaScript disabled or not available will not receive the desired effect. Furthermore, you lose whatever edge you might have otherwise gained from search engines by having images they can index and alt text they can read.

    Given the fact that PHP rotators are lean and handled at the server instead of the client, it's hard to fathom even considering a different option.

    Here's a version I sent to OP via private message:
    http://www.paulhirsch.com/ex/random_rotation/

    The rotator.php file shows the whole thing in action. The .zip file gives you the whole thing you can play with on your own.
     
    the_pm, Feb 12, 2006 IP