Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Inserting a text randomizer - how?

My site is based on a CSS template and built using NVu. On the index/homepage I wanted to put a randomised piece of text in a three column table between two thumbnails. I found some sample code on the web and following the example put in the text I wanted. It doesn't work . :mad:

Here's the code as I've constructed it:

<td style="width: 315px; text-align: center; vertical-align: middle;"> <big><span
style="font-style: italic;"></span></big>
<script language="JAVASCRIPT"><!--var r_text = new Array () ;
r_text[0] = "My husband and I had a wonderful stay at Singapore Deep. It was just wonderful to go and recharge the batteries.
We loved the view of the bay. Laura";
r_text[1] = "Wonderful couple of days,
you should be very proud of your setting and service. Many Thanks, Peter";
r_text[2] = "We had a really lovely 1st anniversary staying at Singapore Deep. We loved the wide sliding doors over looking the water. Sarah";
r_text[3] = "Margaret - thanks again for a wonderful 4 days at your retreat, it was great to meet you. Simon";
r_text[4] = "What an unusual and pleasant two days Paula and I enjoyed this week at Singapore Deep. We were very impressed indeed, not only with the architecture and view but with the cleanliness and high standard of fittings, equipment etc. Ian";
r_text[5] = "Just wanted to say thank you for providing such beautiful accommodation in SD1. We have just had a lovely weekend there enjoying the stunning view and feeling completely relaxed.
You provide just the right combination of privacy and luxury to make the whole experience a joy. Leanne";
r_text[6] = "I would like to let you know that my husband and I had wonderful day at Singapore Deep. We loved the seclusion and the gentle view of the bay. Laura";
var i = Math.round(6*Math.random());document.write(r_text[i]);//--></script></td><td
style="vertical-align: middle; white-space: nowrap; width: 175px; text-align: right;"><a
target="_blank" href="SD_2.html"><img
style="border: 0px solid ; width: 184px; height: 122px;"
title="Click to go to SD 2 page." src="SDTwoLHview52WA07a.jpg"
alt="Singapore Deep Two - 2 bedroom FSC accommodation"
hspace="120"></a></td> </tr></tbody></table>

I've tried to make the JavaScript snippet bold, but everything seems to be bold - sorry.
Can anyone spot the problem or point me to another forum if this isn't the appropriate one.
[2618 byte] By [CliveO] at [2007-11-11 10:11:46]
# 1 Re: Inserting a text randomizer - how?
You can't write to an existing page with document.write. That opens a fresh document, it won't write to an existing one. Best advice is to abandon that script and search for one that writes to an existing element on the page, using innerHTML or DOM methods.
JPnyc at 2007-11-11 23:34:25 >
# 2 Re: Inserting a text randomizer - how?
This code works fine if you place each of your r_text quotes on a single line, no matter how long it is.

The previous answer is incorrect: "document.write" writes to the current document.
bschaettle at 2007-11-11 23:35:26 >