Web Design

Web Design - JavaScript

::: Random Number and Arrays

Refresh this page to change the image

The Code:

<script language="JavaScript">
banners = new Array()
banners[0]="<IMG SRC=images/bnr_jbr1.jpg>"
banners[1]="<IMG SRC=images/bnr_jbr2.jpg>"
banners[2]="<IMG SRC=images/bnr_jbr3.jpg>"
banners[3]="<IMG SRC=images/bnr_jbr4.jpg>"
banners[4]="<IMG SRC=images/bnr_jbr5.jpg>"
banners[5]="<IMG SRC=images/bnr_jbr6.jpg>"
banners[6]="<IMG SRC=images/bnr_jbr7.jpg>"

var RangeLimit = 6
var Number = Math.round(RangeLimit * Math.random());
var TheImage = banners[Number]


Place the code where you want the image to be displayed

See 2 implementations: Sample #1 Sample #2

::: Math.round

returns a number between 0 and the specified Range Limit.

The text fed to document.write can be built with additional arrays (ie. an array holding href values for each image). Use plus sign to concatenate variables.

var TheLink = Links[Number]
document.write("<CENTER><A HREF=" +TheLink+ ">" +TheImage+ "</A></center>")

Back to Web Design Resources