Monday, December 2, 2013

How To: Display a random quote on your website that changes over time

The following block of code can be inserted wherever you want it, and produces a random quote/bit of text that changes over time.

Code:
<SCRIPT type="text/javascript">
var phrases = new Array()
phrases[0]= "[Quote A]"
phrases[1]= "[Quote B]"
phrases[2]= "[Quote C]
phrases[3]= "[Quote D]
phrases[4]= "[Quote E]
function display()
{
a=Math.floor(Math.random()*phrases.length)
document.getElementById('phrase').innerHTML=phrases[a]
setTimeout("display()",5000)
}
</SCRIPT>

<div id="phrase">
<SCRIPT type="text/javascript">display()</SCRIPT>
</div>
</div>

Where "phrases[#]" is the number of the quote you want to insert, and you can add as many as you like.

Example: