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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Randomly change image based on directory contents???

Status
Not open for further replies.

Fuzemmo

Programmer
May 16, 2002
52
0
0
US
Is it possible to do the following?

I have an image with say an id="sponsor" on my web page. I need to be able to randomly pick a new image to display in that spot say every 20 seconds. I pick the image from a list of jpg files in the imgSponsors directory on the server. Is it possible for me to get that directory list on the server, and then client-side just swap the image every N seconds?

Thanks,
Jim
 
something like this ought to do it... you would need to generate the "imgs" array server-side:

Code:
var imgs = new Array(
	"a.jpg",
	"b.jpg",
	"c.jpg",
	"d.jpg",
	"e.jpg"
);

function swapImg() {
	var img = imgs[ Math.round(Math.random() * (imgs.length - 1)) ];
	document.getElementById("sponsor").src = "imgSponsors/" + img;
}

-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
i like your sleeves...they're real big
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top