Click to See Complete Forum and Search --> : Random images at refresh
René123
04-29-2005, 06:48 PM
How could I make it so an image changes into another random image from a list or something after refreshing or every time you go to the page.
Example:
http://www.fishinstitution.com
(see top left corner)
UR_Maximus
04-29-2005, 06:52 PM
I am not too sure if this will work. I usually use this code to do something similar however i used it for random quotes not pictures, however I am sure it would still work. Just replace the quote with the image source! maybe not! either way it is a cool piece of code.
<SCRIPT language=JavaScript>
<!-- Begin
var howMany = 4
var quote = new Array(howMany+1)
quote[0]="quote1"
quote[1]="quote2"
quote[2]="quote3"
quote[3]="quote4"
quote[4]="quote5"
quote[5]="quote6"
function rndnumber(){
var randscript = -1
while (randscript < 0 || randscript > howMany || isNaN(randscript)){
randscript = parseInt(Math.random()*(howMany+1))
}
return randscript
}
quo = rndnumber()
quox = quote[quo]
document.write(quox)
// End -->
</SCRIPT>
Hi René,
You’re going to need some sort of server-side processing instruction in order to do what you want. You could do it in JavaScript, but it would be inaccessible (not work for ~10% of your visitors). It’s also more elegant to avoid JavaScript when it is unnecessary. If you have access to a server-side language, such as Perl, PHP, or ASP, then you can use that. Do you know what server-side languages you have available?
René123
04-29-2005, 07:41 PM
Well, I`m using php right now but I only really understand how to make a comic archive thing.
http://www.gutterflycomix.com/SD
I'll try the java script for now.
If you have PHP available, then you can try this.
<?php
$images = array("image1.gif", "image2.gif", "image3.gif");
$dir = "http://www.site.com/path/to/images/";
# No need to change anything past this line
echo '<img src="'.($images[rand(0,count($images)-1)]).'" alt="">';
?>
With PHP, you can also make a script to take all the images in a directory (on your server, of course) and output one of them at random. That way, you’ll never even have to update the script; you just upload the new image that you want to randomly display, and the script will display it at random.
BonRouge
04-29-2005, 09:56 PM
There's a good article on A List Apart (http://www.alistapart.com/articles/betterrotator/) about this.