Go Back   Web Hosting UK Forums | Linux Windows Dedicated Server and cPanel VPS Hosting Forum > Services > Web Designing and Development

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-29-2007, 01:47 PM
Queenie's Avatar
Member
 
Join Date: Jun 2007
Location: On your head
Posts: 48
Default Random image script

Random Image Display Using JavaScript

Look for those step below:

1. Initializing an array
2. Storing the image file names in that array
3. Finding the length of this array
4. Using the Math.random() to generate a random number
5. Using the randomly generated number as index for retrieving an image file name from the array.
6. Displaying the image.


var img_name = new Array("purple.gif", "red.gif",
"blue.gif", "yellow.gif", "green.gif", "pink.gif");

var l = img_name.length;

var rnd_no = Math.round((l-1)*Math.random());

document.r_img.src = img_name[rnd_no];

NAME="r_img" ALT="Random image">


Note: Array are zero-indexed, thus, we have to decrease the value of variable l (length of the array) by 1 when generating a random number.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 09:59 PM.
Copyright 2002-2007 WebHosting.uk.com. All rights reserved.
Web Hosting UK Forum