Page 1 of 1

rand images from root directory

Posted: Tue Nov 24, 2009 11:11 am
by iknowu99
Hi everybody,

I'm new to Java script. Maybe some of you experts can help?

i'm trying to use the following code i found online but I want it to consider all the jpg files in this directory images/rand/ and open one randomly....there are about 1000 fotos and i want them displayed at random every time the user goes to my site:)

Code: Select all

<SCRIPT LANGUAGE="JavaScript">
var theImages = new Array()
 
//Random images...maybe some loop to consider all jpg files in images/rand/ directory??
theImages[0] = 'images/rand/1.jpg' // replace with names of images
theImages[1] = 'images/rand/2.jpg' // replace with names of images
theImages[2] = 'images/rand/3.jpg' // replace with names of images
theImages[3] = 'images/rand/4.jpg' // replace with names of images
theImages[4] = 'images/rand/5.jpg' // replace with names of images
theImages[5] = 'images/rand/6.jpg' // replace with names of images
theImages[6] = 'images/rand/7.jpg' // replace with names of images
theImages[7] = 'images/rand/8.jpg' // replace with names of images
theImages[8] = 'images/rand/9.jpg' // replace with names of images
 
 
 
 
var j = 0
var p = theImages.length;
var preBuffer = new Array()
 
for (i = 0; i < p; i++){
preBuffer[i] = new Image()
preBuffer[i].src = theImages[i]
}
var whichImage = Math.round(Math.random()*(p-1));
 
function showImage(){
{
document.write('<a href ="link.html"><img src="'+theImages[whichImage]+'" border=0 width=452 height=181></a>');
}
}
 
</script>

i'm using this to show image:

Code: Select all

<script>showImage();</script>

Re: rand images from root directory

Posted: Tue Nov 24, 2009 11:19 am
by iankent
# //Random images...maybe some loop to consider all jpg files in images/rand/ directory??
you'd be better off asking the questions in the post rather than in code, took me a while to work out what you were even asking!!

first off, you won't be able to do the loop with javascript as it wont be able to get a directory listing of the remote server (your server is 'remote' in this case because javascript code is executed on the client)

so, you'll need to use PHP to get the contents of the folder, and output it as a javascript array, e.g.

Code: Select all

<SCRIPT LANGUAGE="JavaScript">
var theImages = new Array();
<?
$counter = 0;
$d = opendir('/local/path/to/folder/');
while($f = readdir($d)) {
    $counter++;
    ?>theImages[<?=$counter?>] = '$f';<?
}
.....
 
all that does is opens the directory using opendir, then loop through the filenames it contains using readdir(). Each result of readdir() is output as the next element in theImages array.

btw, you should also use a semi-colon at the end of every javascript line of code like you do in PHP. also, for full compatibility you should use <script type="text/javascript"> instead of <script language="javascript">

hth :)

Re: rand images from root directory

Posted: Tue Nov 24, 2009 11:22 am
by superdezign
Why even use JavaScript if you only select one image per page load?