Javascript Random Quote Generator

Note: This is targeted to users of iBlog, but should work easily with any site.

Everytime I find a interesting quote I’ve wanted to rotate them through my site. I think there are some free services out there that will do them with a simple javascript but I hate having to have stuff hosted on other sites; it slows mine down. I found this site with some embedded javascript code that does basicly what I want except I dont’ want it embedded. So I modified it to be a file that I referece from within the templates in iBlog. It also has the advantage that when I want to update the quotes I only have to update one file, rather than every template file and republish every page of the stie.

The code looks like this:

/*
This Script was created by Daniel Standley
Please feel free to use it but make sure you give me credit
My Email is gandalf_au@kooee.com.au
and my website is standleydj.tripod.com
*/
// The quotes that appear on the page.
quotes = new Array("<i>&quot;I think we ought to raise the age at which juveniles can have a gun.&quot;</i>&nbsp;- George W. Bush","<i>&quot;They misunderestimated me.&quot;</i> - George W. Bush","<i>&quot;I have no special talents I am only passionately curious&quot;</i> - Albert Einstein","<i>&quot;The report of my death was an exaggeration&quot;</i> - Mark Twain","<i>&quot;One began to hear it said that World War I was the chemists' war, World War II was the physicists' war, World War III (may it never come) will be the mathematicians' war&quot;</i> - Davis, Philip J. and Hersh, Reuben", " "&quot;They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety&quot;</i> - Benjamin Franklin")

//Alter nothing below the script wont work if you do.
qte = quotes.length
function chooseQuote(){
randomNum = Math.floor
((Math.random() * qte))
document.write(quotes[randomNum])

}

The actual quotes are contained in the part that says quotes = new Array( ). You will see some comma seperated values in quotes. You can add or change the ones that are there

To use it take that text and paste it into a text editor. Save it as a plain text file called quotegenerator.js and put it where ever you publish your blog to. You will need to put a line in the template files you wish to use it in.

Put this inbetween the head tags:

<script type="text/javascript" src="<$>quotegenerator.js" language="JavaScript"></script>

To use the quote generator put this bit of code in your site:

<script type="text/javascript">chooseQuote();</script>

I put this in my blog description portion of iblog so it looks like this: 

 

Since iBlog doesn’t use a javascript function for the description it works perfectly. 

Advertisements

6 Replies to “Javascript Random Quote Generator”

  1. Much better now. And very cool.
    Might be fun to do a similar thing with links–a new wacky site to visit every time you drop in. Lord knows, there are lots of them.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s