| Here is a small function that allows random images to be displayed on an ASP document. Although this is an easy task to achieve in ASP, or even using client-side JavaScript, the VBScript function shown here has the advantage of not needing to know the number of images within the folder – it will take account of images being to added or removed from the images folder.
The function uses the FileSystemObject object to access the file system. It checks that the specified image folder exists, and then extracts a list of files within the folder. A link to one of the images (chosen at random) is then returned.
There is a certain amount of error checking within the script; it will return an error message if it cannot find the images folder, and also if it cannot find any appropriate image files.
The function can be called using the following VBScript:
<%Response.Write RandomImage("images/", "gif jpeg", "My Image")%>
The function has three arguments. The first (ImagesFolderPath) is the name of the folder containing the images, and it should be a sub-folder of the folder that contains the ASP document. The second argument should be a string containing a list of file types to use as images (e.g. jpeg, jpg, gif). Finally, the last argument is a short sentence to describe your image (which is used as the image’s ALT tag). |