Displaying a Random ImageIf 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!). Script 4.18. This simple HTML creates the page for a random image.
Script 4.19. You can display random images on your page with this script, which uses JavaScript's Math.random method to generate a random number.
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:
|