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.
| SitePoint Sponsor |
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.




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:
VinnyCode: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.....
moderator at:Webxpertz Forums


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