Forum Moderators: open

Message Too Old, No Replies

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

         

hound_dog

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

10+ Year Member



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!

MichaelBluejay

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

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



<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.