Random images & links

Displaying a Random Image and Hyperlink

Want to randomly display images and hyperlinks from a list? No problem, you can do it with a simple script like the one shown here. No need to learn server-side scripting, or slow down your pages with full-blown Java applications.

The script shown below randomly loads an image from the list you input each time a page is loaded into a browser. This example works with two images. You can add as many as you like. You can even add the same image twice to double its weighting. For example, if you have two images and would like one to be shown twice as much as the other one, simply add the preferred image twice (to total three images in all).

Here's the Script Code. All you have to do is replace the variable "imagenumber" with the correct amount of images you're using. Be sure to label each image with its corresponding number in the images array. Then do the same for the corresponding links. You can also control your image attributes in the display portion of this script, which begins with "document.write" ...

That's it. Load in your info, place the code where you want the images/links to display, refresh the page and watch what happens!

<SCRIPT LANGUAGE="JavaScript">
<!-- Hide
var imagenumber = 2 ;
var randomnumber = Math.random() ;
var rand1 = Math.round( (imagenumber-1) * randomnumber) + 1 ;
images = new Array
images[1] = "Directory/First_Image_Name.jpg"
images[2] = "Directory/Second_Image_Name.jpg"
var myimage = images[rand1]
links = new Array
links[1] = "Directory/Your_Page.html"
links[2] = "Directory/Your_Page.html"
var mylink = links[rand1]
document.write('<A HREF="' + mylink + '"><IMG src="' + myimage + '"></A>')
// -- End Hiding -->
</SCRIPT>


Naturally, you'll have to do a bit of editing to get your own images and links in place. Be sure to keep the imagenumber in sync with however many images and links you wish to use.

 

 

Links

Resources