Display random images each time a page loads or is refreshed
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

All ColdFusion Tutorials By Author: Ryan BeMiller
  • Display random images each time a page loads or is refreshed
    This tutorial will show you how easy it is to display an unlimited number of images randomly on your pages. Each time a page is loaded or refreshed a random image will load.
    Author: Ryan BeMiller
    Views: 27,154
    Posted Date: Tuesday, April 22, 2003
  • Create dynamic PDFs on the fly, and email them too
    Create dynamic PDF's on the fly and automatically send them as attachments. There are a bunch of people out there that claim to have this elusive process figured out, or to have a wonder-tag that does it all for you. Well, I tried a lot of them, and the results were always less than desirable, if you want to call them results. However, through this trial and error, I was able to take the good points of some of these failed attempts, and piece them together into a working solution. Right off the bat, this is a solution for those of you who have the ability to install software on your own hosting server or, those of you who can convince your web host to install a little harmless application for you.
    Author: Ryan BeMiller
    Views: 33,159
    Posted Date: Sunday, May 25, 2003
  • The easiest method for multiple dynamic and dependant drop down lists, period.
    The first EASY method for creating multiple, dependant, database driven drop down select boxes... Honestly!
    Author: Ryan BeMiller
    Views: 45,298
    Posted Date: Sunday, July 27, 2003
  • The Easiest Method for "Previous | Next" Recordset Navigation (aka - Recordset Paging)
    This tutorial provides a simple and quick way for CFers to add Recordset Navigation to their websites. This is the easiest way I could come up with to accomplish the "Previous Page | Next Page" kind of navigation that is needed by so many, yet understood by so few.
    Author: Ryan BeMiller
    Views: 24,137
    Posted Date: Wednesday, September 15, 2004
  • Slick Little Tell-A-Friend Feature
    Add this one page tell-a-friend file to your website and begin letting your visitors promote your site for you. Very easy to set up. Can be cfincluded anywhere on your site. Visitors just fill in their friends email address and click the send button. The rest is taken care of. Simple one click functionality for your visitors. Automatically sends an email, containing anything you wish, to your visitors friend.
    Author: Ryan BeMiller
    Views: 15,105
    Posted Date: Thursday, January 13, 2005
Download the EasyCFM.COM Browser Toolbar!