Random Text Displays

One of the most frequently requested tips is for a "random text" generator.
Uses for such a script are many and include a Quote of the Day or other such randomly selected information. The following JavaScript will perform such a task with ease. Have a look at the script below. We can place the following in the head section of our page, or remotely source it to another directory as a file include.

(Note: we have removed the html comments, as email often plays havoc with them; it's still a good idea to comment your scripts so that older browsers won't display the actual script. See the online archived versions for the full script with commenting in place.)

<SCRIPT LANGUAGE="JavaScript">
var textnumber = 2 ;
var randomnumber = Math.random() ;
var rand1 = Math.round( (textnumber-1) * randomnumber) + 1 ;
text = new Array
text[1] = "To be or not to be, that is the question. -- Shakespeare"
text[2] = "Sin is its own punishment. -- Freud"
var mytext = text[rand1]
</SCRIPT>

You can add entries, but be sure to increase the textnumber variable to match the total number of entries, as well as incrementing the text[2] with the next in the series...

Now, to display our random text, we call the script by executing the following document.write statement.

<script language="JavaScript">
document.write(mytext)
</script>

You can re-write this script to load images, or hyperlinks, or both (images that link to other pages). See this past newsletter for more on this.
http://www.bravenet.com/reviews/archives/tips.php?file=tips/2000_02_01.txt
 

Taken from " More Webmaster Tips and Tricks " Monday 4 December  2000, published by Bravenet  Enrol on the e-mail list at Bravenet
 

Example of a Random Text Display


Return to Home page