PDA

View Full Version : Random Image



Mike Phillips
11-12-2005, 03:15 PM
Hi, I have really been struggling with.
Basically I want to have a script that when the page is opened a random image is displayed.

So that each time the page is refreshed a new image is displyed...

I'm new to this sort of thing, so far all I have been able to come up with is...

var loadimage=new Array()
loadimage[0]=["photo1.jpg", "http://www.google.co,uk", "_new"]
loadimage[1]=["photo2.jpg", "http://www.google.co.uk", "_new"]
loadimage[2]=["photo3.jpg", "http://www.google.co.uk", "_new"]




But I dont know what I can add to make it randomly chose from these and if this would even make the image load on start up and stop, or continue to scroll.

Please, any help would be much appreciated
Mike

jwd
11-12-2005, 04:26 PM
I found this a while back .... check it out it may help.

http://willmaster.com/possibilities/archives/wmp20020402001.shtml

looks like what you are trying to do.

Mike Phillips
11-13-2005, 04:09 PM
That does look great, but unfortunately I can't change the name of my images as they are externally linked from other sites. Is there anyway I could edit the code so that I could reference the images and it randomly selects the reference, if that makes sense?

For example:


<script type="text/javascript" language="JavaScript">

var image1=new Array()
image1[1]='<a href="http://www.site.com/image_1.jpg"> logo1</a>'
image1[2]='<a href="http://www.site.com/image_19.jpg"> logo2</a>'
image1[3]='<a href="http://www.site.com/image_5.jpg"> logo3</a>'
image1[4]='<a href="http://www.site.com/image_99.jpg"> logo4</a>'

NumberOfValuesToRotate = 4;

FirstPart = '<image1[';
LastPart = ']>';

function returnValue() {
var r = Math.ceil(Math.random() * NumberOfValuesToRotate);
document.write(FirstPart + r + LastPart);
}

/////then i should have a random reference name... but I'm not sure if i can use that to call up the referenced image...

</script>

Mike Phillips
11-13-2005, 04:41 PM
Or I found this sort of script on the internet, but I can't seem to get it to work. Bascially the images are ads and use a javascript. I want the the page to randomly chose an ad to display on the page, out of a selection of about 5.


I have tried to get this script to work, but it refuses to load. Can anyone spot where I have gone wrong? Or suggest an alternative?
<script language="JavaScript">
<!-- Copyright 2000 by William Bontrager
function getcontent() {
var count = 5;
var r = Math.ceil(Math.random() * count);
if (r == 1) return "<script language=/"javascript/">var uri = 'http://impgb.tradedoubler.com/imp/img/12568751/1164837?' + new String (Math.random()).substring (2, 11);document.write(/'<a href=/"http://clkuk.tradedoubler.com/click?p=23943&a=1164837&g=12568751/" target=/"_blank/"><img src=/"/'+uri+/'/" border=0></a>');</script>";
if (r == 2) return "<script language=/"javascript/">var uri = 'http://impgb.tradedoubler.com/imp/img/12568751/1164837?' + new String (Math.random()).substring (2, 11);document.write(/'<a href=/"http://clkuk.tradedoubler.com/click?p=23943&a=1164837&g=12568751/" target=/"_blank/"><img src=/"/'+uri+/'/" border=0></a>');</script>";
if (r == 3) return "<script language=/"javascript/">var uri = 'http://impgb.tradedoubler.com/imp/img/12568751/1164837?' + new String (Math.random()).substring (2, 11);document.write(/'<a href=/"http://clkuk.tradedoubler.com/click?p=23943&a=1164837&g=12568751/" target=/"_blank/"><img src=/"/'+uri+/'/" border=0></a>');</script>";
if (r == 4) return "<script language=/"javascript/">var uri = 'http://impgb.tradedoubler.com/imp/img/12568751/1164837?' + new String (Math.random()).substring (2, 11);document.write(/'<a href=/"http://clkuk.tradedoubler.com/click?p=23943&a=1164837&g=12568751/" target=/"_blank/"><img src=/"/'+uri+/'/" border=0></a>');</script>";
if (r == 5) return "<script language=/"javascript/">var uri = 'http://impgb.tradedoubler.com/imp/img/12568751/1164837?' + new String (Math.random()).substring (2, 11);document.write(/'<a href=/"http://clkuk.tradedoubler.com/click?p=23943&a=1164837&g=12568751/" target=/"_blank/"><img src=/"/'+uri+/'/" border=0></a>');</script>";
} //-->
</script>

Mike Phillips
11-13-2005, 05:34 PM
Thanks for your help jwd, but I have managed to work it out with some help from my mate.

Thank you

jwd
11-13-2005, 07:22 PM
I seen a lot of examples like the one I pointed you to, but they all seem labor intensive... (conversion of the file names, sizes, etc). A better idea would be to:

1. have a folder that contained the images.
2. a routine to get the number of items in the folder.
3. generate a random number based on the number of items.
4. get the Nth(random item) file in the folder.
5. verify that its an image file (if not get another one or the next on n+1)
6. pass the path and item name to the display routine.
7. the display routine also needs to size the image.

then you could just add new images to the folder and not have to rewrite the script each time.