Displaying a Random Image

If your site is rich with graphics, or if you are displaying digital artwork, then you may want to have a random image from your collection appear when the user enters your site. Once again, JavaScript to the rescue! The extremely simple Script 4.18 shows the required HTML, and Script 4.19 provides the JavaScript. Figure 4.15 shows the result of the script, in this case images of a stuffed lion, tiger, and bear (oh, my!).

Figure 4.15. Depending on the value of the random number generated by the script, the user is presented with the lion, the tiger, or the bear.

To display a random image:

1. ...

Get JavaScript and Ajax for the Web: Visual QuickStart Guide, Seventh Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.