Random Images On An Html Page

Tech questions of any kind can be asked here.
Forum rules
Forum Specific Rules
So you got questions about audio/video? Wanna know how to rip a digital stream? Make an X/S/VCD to play on your DVD player? Maybe rip a video from VHS or TV? Find the answers here.
User avatar
Leighton
That Was Just Your Thousandth Post
Posts: 1963
Joined: Thu Jan 20, 2005 3:41 am
Location: In a Snakepit
Country: Australia

Random Images On An Html Page

Post by Leighton »

I've checked a few sites that try to help out with displaying a random image on a page, but so far I've had no luck. All the places I've seen have said put this text here, this text there, replace the text with your filenames and it'll work. But it ain't happening so far. I'd love just a quick and easy instruction and I hope someone here can help me.

Thanks in advance!! :thumbs-up:
NEW WEBSITE UNDER CONSTRUCTION!!
METALLICA, INC.
Last updated 2012-06-28

Leighton Bootlegs*
Last updated 2010-02-26
*Old website
User avatar
Euge
Trapped Under 800
Posts: 656
Joined: Sat Nov 20, 2004 8:35 pm
Location: Republic of Ireland
Country: Ireland

Re: Random Images On An Html Page

Post by Euge »

Code: Select all

<script language="JavaScript"><!--

//Javascript Created by Computerhope http://www.computerhope.com

//store the quotations in arrays

images = new Array(4);

images[0] = "<a href = 'http://www.computerhope.com/index.htm'><img src='http://www.computerhope.com/banners/banner.gif' alt='Visit Computer Hope'></a>";

images[1] = "<a href = 'http://www.computerhope.com/history/index.htm'><img src='http://www.computerhope.com/banners/banner2.gif' alt='Computer History'></a>";

images[2] = "<a href = 'http://www.computerhope.com/index.htm'><img src='http://www.computerhope.com/banners/banner3.gif' alt='Visit Computer Hope'></a>";

images[3] = "<a href = 'http://www.computerhope.com/newslet.htm'><img src='http://www.computerhope.com/banners/banner4.gif' alt='Computer Hope Newsletter'></a>";

index = Math.floor(Math.random() * images.length);

document.write(images[index]);

//done

// --></script><br>
I got that to work for me, see if it'll work on your site, and just copy the images[1] bits to have more pictures
User avatar
Leighton
That Was Just Your Thousandth Post
Posts: 1963
Joined: Thu Jan 20, 2005 3:41 am
Location: In a Snakepit
Country: Australia

Re: Random Images On An Html Page

Post by Leighton »

Looks like that works. I'll make some new banners and whack them up soon.
NEW WEBSITE UNDER CONSTRUCTION!!
METALLICA, INC.
Last updated 2012-06-28

Leighton Bootlegs*
Last updated 2010-02-26
*Old website
User avatar
frantik2k3
That Was Just Your Thousandth Post
Posts: 2330
Joined: Mon Jun 16, 2003 4:41 am
Location: New Hampshire, USA
Country: United States

Re: Random Images On An Html Page

Post by frantik2k3 »

Javascript will get the job done, however, it might fail to work the way you want it to when people view your site using different browsers. You will see a huge difference when viewing your pages in IE and then in FireFox.

Just a heads up on that.

If something breaks in a certain browser, you can usually find a snipbit of code that will correct it. Google is a good source for that stuff.
:: Frantik2k3 Trading Website :: hosted by Metpage!
User avatar
Speed Damage
Phantom Lord
Posts: 938
Joined: Tue Apr 01, 2003 12:30 am
Location: Wisconsin
Country: United States

Re: Random Images On An Html Page

Post by Speed Damage »

I think this is pretty much the same as what Euge posted, but I know this worked for me:

Code: Select all

var mess = new Array(
'<img src="banner/banner_01.jpg">',
'<img src="banner/banner_02.jpg">',
'<img src="banner/banner_03.jpg">',
'<img src="banner/banner_04.jpg">',
'<img src="banner/banner_05.jpg">',
'<img src="banner/banner_06.jpg">',
'<img src="banner/banner_07.jpg">',
'<img src="banner/banner_08.jpg">',
'<img src="banner/banner_09.jpg">',
'<img src="banner/banner_10.jpg">');
var max = mess.length;
var num = Math.floor((Math.random() * max));
document.writeln(mess[num]);
(change the file names to you images, thats just what I was using)

save that as a javascript (random.js for example) then just insert that where you want the random image with "<script type="text/javascript" src="random.js">"
User avatar
slayerdiabolus
Stone Cold Poster
Posts: 3207
Joined: Sat Oct 30, 2004 8:34 pm
Location: Cork, Ireland
Country: Ireland

Re: Random Images On An Html Page

Post by slayerdiabolus »

I was asking Spawn the very same question last week. thanks for this!!
Image
2003 - Dublin. 2004 - Donington, Dublin. 2006 - Donington. 2007 - Live Earth, Wembley. 2008 - Dublin, London. 2009 Milan, Rome, Dublin