Welcome to WebmasterWorld Guest from 3.227.249.234

Forum Moderators: open

Message Too Old, No Replies

I have a random image script - I'd like to add a caption feature

     
6:59 pm on Sep 3, 2006 (gmt 0)

Junior Member

10+ Year Member

joined:Mar 18, 2003
posts:68
votes: 0


Would really appreciate someone's input here please. I have a small piece of Javascript for a random image. I would now like to extend it by adding in a 'caption' feature, so that when the random image is generated, the corresponding caption is also displayed.

Here's the script:


<script language="JavaScript" type="text/javascript"> <!-- function random_img(){ var ranimage = new Array(); ranimage[1] = "photo1.jpg"; ranimage[2] = "photo2.jpg"; ranimage[3] = "photo3.jpg"; var ry = Math.floor(Math.random()*ranimage.length) if (ry==0) ry=1 document.write('<img src="'+ranimage[ry]+'" border=1>'); } random_img(); // --> </script>

So that display a random image. Now, say photo1.jpg is displayed - I would also like a caption for that photo to show up below the photo.

How do I amend the js so that I can do this?

Thanks for any help!

2:19 am on Sept 4, 2006 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month

joined:Feb 12, 2003
posts:1208
votes: 0


<script language="JavaScript" type="text/javascript"> <!-- 
function random_img(){
var ranimage = new Array();
ranimage[1] = "photo1.jpg";
ranimage[2] = "photo2.jpg";
ranimage[3] = "photo3.jpg";
[red] captions = ['caption 1','caption2','caption3'];[/red]

var ry = Math.floor(Math.random()*ranimage.length);
if (ry==0) ry=1; document.write('<img src="'+ranimage[ry]+'" border=1>'[red]+'<br>'+captions[ry][/red]);

}
random_img();
// --> </script>

-------------------------------------
I haven't tested this, but if it doesn't work, it should be pretty close and need just a little tweaking.