Display random images each time a page loads or is refreshed.

An easy way to keep the look of your site fresh is to display a different image each time the page is refreshed or loaded. Accomplishing this is a very simple task.

Say you want your header image to be the random image. First create a directory for the header images in your image directory.
ex - images/header/

Let's say we're going to swap between 4 images that are stored in our images/header/ folder. They are named 1.jpg , 2.jpg , 3.jpg , and 4.jpg

On your header cfm page you'll need two things.

First a <cfparam> statement like the following: <cfparam name="imageswap" default="#randrange(1, 4)#">

... name it whatever you want. The randrange() is the key here. Change the 4 to whatever number of images you want to swap between.

Next, an if statement:
<cfif imageswap is "1">
   
<img src="images/header/1.jpg" width="539" height="119" border="0">
<cfelseif imageswap is
"2">
  
<img src="images/header/2.jpg" width="539" height="119" border="0">
<cfelseif imageswap is
"3">
   
<img src="images/header/3.jpg" width="539" height="119" border="0">
<cfelseif imageswap is
"4">
   
<img src="images/header/4.jpg" width="539" height="119" border="0">
</cfif>

Or you can do this:
<cfoutput>
   
<img src="images/header/#imageswap#.jpg" width="539" height="119" border="0">
</cfoutput>


That's it, they'll load randomly. You can do this with an unlimited number of images.

Enjoy!

Look for more interesting ideas at pushcode.com

About This Tutorial
Author: Ryan BeMiller
Skill Level: Beginner 
 
 
 
Platforms Tested: CF5,CFMX
Total Views: 41,787
Submission Date: April 22, 2003
Last Update Date: June 05, 2009
All Tutorials By This Autor: 2
Discuss This Tutorial
  • Lol, thanks a bunch for this, it was the first link on google. so i clicked it, read it, and im amazed at how easy it is, once again, thanks a bunch

  • love the feature but cant get it to work with swf files... would you be so kind as to give us a sample how to do this?

  • Eduardo, I don't see why it wouldn't also work with swf's. Good luck!

  • Nice! Will it work with swf's too?

  • Was looking for something simple that didn't include a database and didn't have to list each image. This is perfect! Thanks!

Advertisement

Sponsored By...
Powered By...