There's a thread from a few years back that talks about creating a random image with link, random text piece of javascript...the code of which can be seen below.

The problem is that the images do not coincide with the text. So an image will show...and the links on the image are correct....but the text is not correct. The quotation acts as a description of the picture.

I've had no luck, so I'm hoping someone can help me figure this out.
I really appreciate the help. :-)
Thank you.

<table width="100%" border="0" cellspacing="1" cellpadding="1">
    <td width="2%">
<script language="JavaScript">
<!-- Hide from old browsers

var imagenumber = 9 ;
var randomnumber = Math.random() ;
var rand1 = Math.round( (imagenumber-1) * randomnumber) + 1 ;

images = new Array
images[1] = "../image/day.gif"
images[2] = "../image/day.gif"
images[3] = "../image/day.gif"
images[4] = "../image/day.gif"
images[5] = "../image/day.gif"
images[6] = "../image/day.gif"
images[7] = "../image/day.gif"
images[8] = "../image/day.gif"
images[9] = "../image/day.gif"
var image = images[rand1]

links = new Array
links[1] = ""
links[2] = ""
links[3] = ""
links[4] = ""
links[5] = ""
links[6] = ""
links[7] = ""
links[8] = ""
links[9] = ""
var link = links[rand1]

var Quotation=new Array() 

Quotation[1] = "tomascook";
Quotation[2] = "airtours.";
Quotation[3] = "clubmed.";
Quotation[4] = "firstchoice.";
Quotation[5] = "thefirstresort.";
Quotation[6] = "jmc.";
Quotation[7] = "thomson.";
Quotation[8] = "nextag.";
Quotation[9] = "portlanddirect.";

document.write('<A HREF="' + link + '"><IMG SRC="' + image + '" border="0"></a>')</script></td>
    <td valign="top" width="98%"><font face="Arial, Helvetica, sans-serif" size="1" color="#000000"> 
      <script language="JavaScript">var Q = Quotation.length;
var whichQuotation=Math.round(Math.random()*(Q-1));
function showQuotation(){document.write(Quotation[whichQuotation]);}

Why would you expect the return value from Math.random( ), to have the same value when the function is called twice? That's kind of the point of Math.random( )...

Store the result of the first call to Math.random ( ) into a global variable, and use that value in all the places that depend on that value... Variables declared in one top-level block of Javasript have the same value in subsequent blocks on the same page. So, in the second block, instead of:

var Q = Quotation.length;
var whichQuotation=Math.round(Math.random()*(Q-1));
function showQuotation(){document.write(Quotation[whichQuotation]);}

just use:


Works better. Thank you.

Any idea why it wouldn't show up in Internet Explorer, other than the fact that IE sucks?!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.