iNET Interactive - Online Advertising Agency
          
   Home    Authors    About    Login    Contact Us
   Search:   
Advanced Search     
  Articles

  ASP (26)
  ASP.NET (19)
  C and C++ (4)
  CFML (2)
  CGI and Perl (16)
  Flash (2)
  Java (7)
  JavaScript (28)
  PHP (92)
  MySQL (13)
  MSSQL (3)
  HTML (34)
  SEO (9)
  Visual Basic (12)
  CSS (13)
  SSI (5)
  XML (12)
  C# (14)

  Developer News

May 17, 2008
Learn HTML forms
About
 
May 16, 2008
Who Are Your Top Friends on Facebook?
About
 
May 15, 2008
Reader Question - Would you host your client's work on your website?
About
 
May 15, 2008
How to Create an Ajax Autocomplete Text Field: Part 6
WebReference.com
 
May 14, 2008
Poll: Are the browser safe colors still needed?
About
 
May 14, 2008
Google Doctype launched
About
 
Courtesy of moreover.com
 
Want to receive new articles via e-mail? Click here!
/Home /JavaScript

Present random fresh images at each page load 

  Views:    7707
  Votes:    4
by Val Zamulin 1/29/04 Rating: 

Synopsis:

This script presents new randomly selected image in the location specified each some the page is loaded (reloaded). I personally like this feature as it creates kinda impression of *freshness* and makes visitors believe you spend more time on updating pages than what's in reality.
Pages: 
The Article

This script allows you to present random images each time the page is loaded or reloaded.

The operations plan is as follows:

Fisrtly you need to show the function the array of images you want to be randomly refreshed at the page load(reload).

The core of the script is the javascript function Math.random() which generates random figure (most likely a decimal fraction with 16 decimal values) in the range between 0 and 1.
if you multiply the decimal fraction by the number that is lower by 1 than the number of arguments in your array (in this case images) you get an algebraic integer that corresponds to the sequence number of each argument (in this case images) in your array.

After that you associate that randomly generated number with a certain image in your array that has that sequence number - and get it uploaded in the place where it belongs.

Sounds complicated? Never fear - I'll explain it again. The default sequence number of the first argument in the array is 0, so if your array contains 5 images the sequence number of the last one will be just 4. But the amount of array arguments (array length) is 5! See the trick here? You can't just multiply the rounded random integer by the amount of array arguments because it this case you might get 5 for which there's NO argument (image in our case). This is why you should always subtract 1 from total number of arguments in the array.

And here comes the code.

Place this piece of code into your <head> section

<script language="Javascript">
function RandomizeImage()
{
var imagesarray = new Array("1.gif", "2.gif", "3.gif", "4.gif")
*/
here you indicate path to all images you want ot partake in this misdemeanor 
*/

var randomnumber = Math.round(Math.random()*(imagesarray.length - 1))
document.images.someimage.src = imagesarray[randomnumber]
}
</script>


You will need to invoke the function at each page load. For this just add onLoad event handler to the <body> tag

<body onLoad="RandomizeImage()">

and this should belong where you want image to appear.

<img src="1.gif" name="someimage">

Note that name here should be the same as what's marked as bold red in the <script> section.

Happy randomizing!!!

Pages: 

Similar/related articles:


 
  Sponsors