Javascript: Display Random Text from a list
Monday, January 5th, 2009Did you ever want to display a random text from a pre-populated list? Maybe you wanted to randomly display a client testimonial from a list of 10… well, here’s the script you need.
place the following code in the head of your document (somewhere between the <head> and </head> tags):
<script type=”text/javascript” src=”../scripts/rotate.js”></script>
now, place the following code in the body of your document, where you want the random text to be displayed:
<script language=”JavaScript”>
<!–
document.write(r_text[i]);
–>
</script>
then, create a javascript document named rotate.js with the following code:
var r_text = new Array ();
r_text[0] = “random text 1″;
r_text[1] = “random text 2″;
r_text[2] = “random text 3″;
r_text[3] = “random text 4″;
r_text[4] = “random text 5″;
r_text[5] = “random text 6″;
r_text[6] = “random text 7″;
r_text[7] = “random text 8″;
r_text[8] = “random text 9″;
r_text[9] = “random text 10″;
var i = Math.floor((r_text.length)*Math.random())
and edit the texts. You can add or delete as many lines of random texts as you wish and they will automatically be included. Then save it in a subdirectory named “scripts”.
That’s all you need!

