Random Quotes

This is a good script to use for placing a random quote on a page. Once the page is loaded, a random quote will be selected and placed where you'd like it to appear.

Open Notepad, copy the following script and save it as quotes.js. Make sure the "Save as type" is set to "All Files":

var quotes=new Array();
quotes[0] = "This is quote 1.";
quotes[1] = "This is quote 2.";
quotes[2] = "This is quote 3.";

var q = quotes.length;
var whichquote=Math.round(Math.random()*(q-1));
function showquote(){document.write(quotes[whichquote]);}
showquote();

Now on the page you'd like to use the random quotes script on, place the following where you want the random quote to appear:

<script language="javascript" type="text/javascript" src="quotes.js"></script>

If you refresh your page, you should get a random quote appearing! If you want to add more quotes, just open the quotes.js file and add as many as you'd like:

quotes[0] = "This is quote 1.";
quotes[1] = "This is quote 2.";
quotes[2] = "This is quote 3.";
quotes[3] = "This is quote 4."; (4th quote added)
quotes[4] = "This is quote 5."; (5th quote added)

« More Web Coding tutorials