random image generator

karmaman's picture

He has: 82 posts

Joined: Nov 2003

Hi I have found the following script, my problem is I don't know any javascript and I have tried to work it out but can't seem to make this script work for me.
I would like to create a folder to store all the images relevent to this page in so that I can keep things neat can some one please help me to understand this script.

<script language="JavaScript">

<script language="JavaScript">

They have: 426 posts

Joined: Feb 2005

Well i dont know exactly however i can try and explain my basic understanding.

&lt;script language="JavaScript"&gt;

<strong>// creating a new array where the images are stored</strong>

var theImages = new Array() // do not change this

<strong>// change the name of these to yours, these are the images in the array</strong>
theImages[0] = '100.jpg'
theImages[1] = '200.jpg'
theImages[2] = '300.jpg'
theImages[3] = '400.jpg'
theImages[4] = '500.jpg'

// ======================================
// do not change anything below this line
// ======================================

<strong>// j is a variable it has been created and set to 0 p is a variable with its value set to the length of the array, in this case 5</strong>
var j = 0
var p = theImages.length;

<strong>// buffer is another array been created and the for loop is looping round the amount of times equal to the length of the array and assigning the new array the image link from the previous array where the images are stored(theimages[i]) theimages being the original array and i being the value of the loop number</strong>

var preBuffer = new Array()
for (i = 0; i < p; i++){
preBuffer[i] = new Image()
preBuffer[i].src = theImages[i]
}

<strong>// here a new variable whichimage is assigned a random number that has been rounded up(i think) times by p(the image array length) not sure about -1</strong>
var whichImage = Math.round(Math.random()*(p-1));

<strong>// this a new function showimage() looks like you need to finish is off, at the moment it writes an empty space.  perhaps the parametre inside the function should be the random number and document.write shoule be the array image.</strong>

function showImage(){
document.write('');
}

//-->

<strong>
function showimage(randomNumber){

document.write(<img src='prebuffer[randomNumber]'>)
}

to call the function:

showimages(whichimage);

it wont work but see what im trying to do, get the random number inside the functions parametres when you call it. the random number refers to the image in the arrays(1-5) trying to get a random number from 1-5??
</strong>

&lt;script language="JavaScript"&gt;
<!--
// ==============================================
// Copyright 2003 by jsCode.com
// Source: jsCode.com
// Author: etLux
// Free for all; but please leave in the header.
// ==============================================

<strong>// this is how to call the function</strong>
showImage();
//-->
'

perhaps someone can help me finish it off and correct my mistakes.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.