Random images on website how to do it

Cameraman

Suspended / Banned
Messages
640
Name
Dave
Edit My Images
Yes
Anyone have any suggestions on how i can get a set of random images to appear on the front page of my website. Im sure you've seen it or even have it were by when a viewer visits your webpage a different image appears from a selected set :thinking:

Dave:thumbs:
 
There are various ways you can do this depending on technology of your server software. For example you could do this with PHP or ASP as a server side script or you could use Javascript, but this would reply on the browser having javascript enabled.

Here is a javascript that I wrote that I use on one of my sites.

This snippet in the <head> section of your site

Code:
<script tyle="text/javascript">
var banner_image = new Array("images1.jpg","images2.jpg");
var banner_url = new Array("image1-url.htm","image2-url.htm");
var banner_alt = new Array("Image 1","Image 2","Image 3");
var index = Math.floor(Math.random() * banner_image.length);
</script>

Put this where you want to display the image

Code:
<script type="text/javascript">
             	document.write("<a href='"+banner_url[index]+"'><img src='"+banner_image[index]+"' border='0' width='100' height='100' alt='"+banner_alt[index]+"' style='margin-top: 6px;' /></a>");
             </script>

There is a bit of styling and image height and width in there, which you can modify for your needs.

The arrays in the head section are
1. The Image URL
2. If you want the image clickable then include a destination URL here (leave black for none)
3. The ALT text for each image.
 
Back
Top