View Full Version : random text?

12-02-2009, 10:38 AM
I have a text file(quotes.txt) with 35+ quotations, separated by a single line break. Is it possible for a DIV to display a random quotation from quotes.txt each time the user visit my home page?

12-02-2009, 10:44 AM
Javascript can't read/write to/from a file in the server. So you need to use some server side code for this.

Philip M
12-02-2009, 11:43 AM
Of course it is possible! With only 35 quotes, there is no problem in using Javascript.

<span id = "quote"></span>

<script type = "text/javascript">
var Quotation = new Array();
Quotation[0] = "Time is of the essence! Comb your hair.";
Quotation[1] = "Sanity is a golden apple with no shoelaces.";
Quotation[2] = "Repent! The end is coming, $9.95 at Amazon.";
Quotation[3] = "Honesty blurts where deception sneezes.";
Quotation[4] = "Pastry satisfies where art is unavailable.";
Quotation[5] = "Delete not, lest you, too, be deleted.";
Quotation[6] = "O! Youth! What a pain in the backside.";
Quotation[7] = "Wishes are like goldfish with propellors.";
Quotation[8] = "Love the river's \"beauty\", but live on a hill."; // note escape quotes
Quotation[9] = "Invention is the mother of too many useless toys.";

var Q = Quotation.length;
var whichQuotation = Math.round(Math.random()*(Q-1));
function showQuotation(){
document.getElementById("quote").innerHTML = Quotation[whichQuotation];



"By three methods we may learn wisdom: First, by reflection, which is noblest; Second, by imitation, which is easiest; and third by experience, which is the bitterest."