SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    May 2003
    Location
    Fontana, Ca
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Loading Different Images each Visit

    I would like to display a different set of images each time a person visits a web site. I have seen this done in many banner advertisments and product photos. Can someone tell me how this can be done with Javascript?

    Thanks.

  2. #2
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Xavier,

    if you mean each individual sees a different image on each visit, you need to use a cookie or dbms to store a record of images already seen.

    if you mean a different image each time the page is visited, you simply need to use a randomizer.

    1) place your images in an array
    2) create a random number
    3) use that number to access the image
    4) display the image

    something like:
    Code:
    var myPics = new Array();
    myPics[0] = 'pic1.gif';
    myPics[1] = 'pic2.gif';myPics[2] = 'somepic.gif';myPics[3] = 'anotherpic.jpg';
    var maxNum = myPics.length;
    var randNum =  Math.floor( Math.random() * maxNum);
    var imgSrc = myPics[randNum];
     
    <img src = imgSrc.....
    Vinny
    Where the World Once Stood
    the blades of grass
    cut me still

  3. #3
    SitePoint Addict
    Join Date
    Oct 2000
    Posts
    304
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There are quite a few scripts around for this.

    Here is one that's easy to implement:

    http://jscode.com/js_random_image.shtml


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
  •