Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Load different gif each time someone visits 1

Status
Not open for further replies.

jwkolker

Programmer
Jan 9, 2003
68
US
I am using dreamweaver - what I would like to do is load a different gif in the same place with each new visit. How do I do this? Any suggestions?

Thanks.

John Kolker

John Kolker
Programmer
jwkolker@comcast.net
 
You could use scripting - either server-side or Javascript - to display a random image each time the page is loaded. Tracking the number of times each particular IP address owner visits your site and displaying a particular sequence of images is probably more trouble than it's worth.

A javascript something like this should do the trick:
[tt]
var theImages = new Array()

Images[0] = '1.gif'
Images[1] = '2.gif'
Images[2] = '3.gif'
Images[3] = '4.gif'
//add more images here ...

function showImage(){
var Pick = Math.round(Math.random()*(Images.length-1));
document.write('<img src=&quot;'+Images[Pick]+'&quot; />');
}
[/tt]



-- Chris Hunt
Extra Connections Ltd

The real world's OK for a visit, but you wouldn't want to LIVE there!
 
Hi Chris - thank you very much - what do I put where? What goes in the head section and what in the body etc...

Thank you very much.

JK

John Kolker
Programmer
jwkolker@comcast.net
 
Actually, I had another look at the script I'm using (to present a random quote, not a picture, but the principle's the same). It's slightly different...

Put the following in a file on its own. Call it, say, &quot;showpic.js&quot;:
[tt]
var theImages = new Array()

Images[0] = '1.gif'
Images[1] = '2.gif'
Images[2] = '3.gif'
Images[3] = '4.gif'
//add more images here ...

var Pick = Math.round(Math.random()*(Images.length-1));
document.write('<img src=&quot;'+Images[Pick]+'&quot; />');
[/tt]
Now, on the web page where you want the image to appear, put this instead:
[tt]
<script language=&quot;JavaScript&quot; src=&quot;showpic.js&quot; type=&quot;text/javascript&quot;></script>
[/tt]
I'm not a JavaScript expert, and haven't tested this, but it should do the trick for most people. You can see my random quote version in action at and .

The advantages of using JS to do the randomising at the browser end are that it's (relatively) simple, possible on freebie hosts as well as paid ones, and the random images won't be cached - ensuring that you get a fresh roll of the dice each time you load the page.

The disadvantage is that people who surf with JS switched off (for whatever reason) will not see your images - the script will be ignored. If you want to be sure your images show up, you'll need to use some kind of server-side scripting. Here's a link to some perl scripts, there's also some remotely hosted services there if you don't have your own cgi-bin...


My McGonagall site uses Javascript for the (eye-candy) quote at the top of each page, but server-side script for the (vital) &quot;Gem of the Day&quot; on certain pages.



-- Chris Hunt
Extra Connections Ltd

The real world's OK for a visit, but you wouldn't want to LIVE there!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top