Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

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:
document.write("<img src=\"" + current_image + "\">")
Steve Renaker
Comment and Contribute






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



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