Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Web
Expertise: Intermediate
Mar 3, 1999

Show a Random Image

A bit of randomness can help keep your Web site from getting stale. You can use the JavaScript rand() function to display random text or a random image.
 
function goRand() {
var num = 3;  // number of images
myImages = new Array();
myImages[0] = "image1.gif";
myImages[1] = "image2.gif";
myImages[2] = "image3.gif";

rand = Math.floor(Math.random() * num);
current_image = myImages[rand];
}
The Math.random() function generates a random number between 0 and 1, and the Math.floor() function rounds it down to the nearest integer. Use the document.write() function to display the randomly generated image:
 
<script>
goRand();
document.write("<img src=\"" + current_image + "\">")
</script>
Steve Renaker
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date