SHARE
Facebook X Pinterest WhatsApp

ASP Example #4 – Random Images

Written By
thumbnail
Joe Burns
Joe Burns
Jan 4, 2005

…use these to jump around or read it all

[The Script]
[Using It]

     Once Again: This tutorial deals with ASP. I do not have the ability to run ASP on my server so you’ll need to take this stuff and run it on yours to get the effect. I have run this on an ASP server so I know it works. If you’re not sure if your server has ASP capabilities, go to the first ASP tutorial and get the little test script to find out.

     When I asked my ASP/VBScript wizard to write some scripts that I could hand out as examples, the one I wanted straight away was an image, or banner, randomizer. It’s a pretty popular effect. When he quoted me the price to write the script, I expected something rather ornate. Nope. He did this one in about 20 seconds. That dirty, rotten…


The Script

     To get the effect, you need the script and a bunch of like images. By “like”, I mean the same size. This script is set up to choose between six different images. It looks like this:

<%

RANDOMIZE
Response.write(“<IMG SRC=’randompicture” & INT((6)*RND()) & “.gif’>”)

%>

(Get the Response.write code all on one line or this won’t work.)

     You’re seeing the script in the shortened form. The original included a lot of extra attributes describing the image:

WIDTH=200 HEIGHT=50 BORDER=0 ALT=’Random Picture’.


Using It

     You might have grasped this already from the script, but the names of these images will all be “randompicture#.gif”. See the INT((6)*RND())? That is code that will randomly choose a number between one and six. Reading it from right to left, the script writes an Image tag to the page and sets the SRC to read “randompicture”, and chooses a random number, and then adds the .gif.

     So, in order to use this script, you paste it in where the random image will go. Then, in the same directory, you offer the images it will need to choose from. In the case of this script, you’ll need six images named randompicture1.gif through randompicture6.gif.

     When the page runs, the user will not see any of this code, just the image command written by it. Of course you can change the name of the image in the script and also add anchor tags to turn the image into a hypertext link. The format is very similar to a JavaScript document.writecommand in that it just writes what’s inside to the page.

 

Enjoy!

[The Script]
[Using It]

Recommended for you...

Parsing Atom Feeds using XPath
Rob Gravelle
Mar 30, 2015
Handling Errors when Processing WP_Ajax_Response XML Data
Robert Gravelle
Aug 11, 2014
Serve Up XML Using WP_Ajax_Response
Rob Gravelle
Dec 4, 2013
Displaying RSS Feeds with XHTML, CSS and JQuery
Michael Rohde
Jul 28, 2011
HTML Goodies Logo

The original home of HTML tutorials. HTMLGoodies is a website dedicated to publishing tutorials that cover every aspect of being a web developer. We cover programming and web development tutorials on languages and technologies such as HTML, JavaScript, and CSS. In addition, our articles cover web frameworks like Angular and React.JS, as well as popular Content Management Systems (CMS) that include WordPress, Drupal, and Joomla. Website development platforms like Shopify, Squarespace, and Wix are also featured. Topics related to solid web design and Internet Marketing also find a home on HTMLGoodies, as we discuss UX/UI Design, Search Engine Optimization (SEO), and web dev best practices.

Property of TechnologyAdvice. © 2025 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.