/    Sign up×
Community /Pin to ProfileBookmark

Random images at refresh

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:
[url]http://www.fishinstitution.com[/url]
(see top left corner)

to post a comment
HTML

5 Comments(s)

Copy linkTweet thisAlerts:
@UR_MaximusApr 29.2005 — 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>
Copy linkTweet thisAlerts:
@JonaApr 29.2005 — [font=trebuchet ms]Hi René,

You&#8217;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&#8217;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?[/font]
Copy linkTweet thisAlerts:
@Ren__123authorApr 30.2005 — 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.
Copy linkTweet thisAlerts:
@JonaApr 30.2005 — [font=trebuchet ms]If you have PHP available, then you can try this.[/font]

[code=php]
<?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="">';
?>
[/code]


[font=trebuchet ms]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&#8217;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.[/font]
Copy linkTweet thisAlerts:
@BonRougeApr 30.2005 — There's a good article on [URL=http://www.alistapart.com/articles/betterrotator/]A List Apart[/URL] about this.
×

Success!

Help @Ren__123 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.8,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...