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">
  <tr>
    <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] = "http://www.thomascook.co.uk"
links[2] = "http://www.airtours.co.uk"
links[3] = "http://www.clubmed.co.uk"
links[4] = "http://www.firstchoice.co.uk"
links[5] = "http://www.thefirstresort.co.uk"
links[6] = "http://www.jmc.co.uk"
links[7] = "http://www.thomson.co.uk"
links[8] = "http://www.nextag.co.uk"
links[9] = "http://www.portlanddirect.co.uk"
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]);}
showQuotation();</script></font> 
</td>
  </tr>
</table>

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]);}
showQuotation();

just use:

document.write(Quotation[rand1]);

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.