Tutorial

Click on thumbnailed images to enlarge

I'm just gonna put the whole chunk of code and heavily comment them out so that we can look at it more detail :)

<script type="text/javascript">
<!--
/*Random Image Script by Herbert Stephen
site: www.bottie.org / mail: me@bottie.org
Thank You for using :)*/

/*First you create an array consisting of paths to your choice of images*/

usr_imgArr = new Array('image1.gif', 'image2.gif', 'mage3.gif');

/*This is the thing that makes 'em random. Make a variable and name it whatever you want to. Then there's the built in round() function and floor() function. Math object is a must :) Math.floor() generates random number from 0 to 1 then we multiply the generated number with the number of array items (3 images). Math.round() wraps up the mess and rounds up the generated number to a whole number.*/

genRandF = Math.round(Math.floor()*usr_imgArr.length);

/*Then we use document.write(""); to generate the image. Note that in src we put the array name joined with a bracketed random engine. That creates a randomly generated image source :) HOW? Because each array item is always represented by a single digit (key number), meaning first in the array list is 0, second is 1, third is 2 then in the src attribute we are actually inserting a key number. Thus, when "1" is generated, we get image2.gif - I guess the rest makes sense, no?*/

document.write("<img border='0' src='"+usr_imgArr[genRandF]+"' alt='random.image' />");
//-->
</script>

Tutorial Comments

Showing latest 2 of 2 comments

gosh thanks for the bugfix. i simply copied it from my javascript generator. i guess my generator's producing error as well. thanks!

By birdman on Jan 26, 2007 7:45 am

This script is incorrect. The problem lies on this line:CODEgenRandF = Math.round(Math.floor()*usr_imgArr.length);That doesn't do anything—it's not even valid JavaScript! It should look like this:CODEgenRandF = Math.floor(M

By mipadi on Jan 1, 2007 1:44 am

Tutorial Details

Author birdman View profile
Submitted on May 24, 2006
Page views 21,452
Favorites 9
Comments 2