ASP Example #4 - Random Images

By Joe Burns

...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:


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.



[The Script]
[Using It]

  • Web Development Newsletter Signup

    Invalid email
    You have successfuly registered to our newsletter.
Thanks for your registration, follow us on our social networks to keep up-to-date