Jump to content
xisto Community
Sign in to follow this  
Avalon

Random Quote Script

Recommended Posts

Here's a little random quote script that you can use to randomly choose from an array of quotes you provide. It's very easy to use on any page as it's an external script, you just call it from your page. I wouldn't use it for any more than about 50 quotes though, it would probably slow your site down too much. Here's the code:


ar[5] = "Little by little, one travels far."

ar[6] = "I don't know half of you half as well as I should like; and I like less than half of you half as well as you deserve."

ar[7] = "It's a dangerous business going out your front door."

ar[8] = "All that is gold does not glitter."

ar[9] = "His house was perfect, whether you liked food, or sleep, or work, or story-telling, or singing, or just sitting and thinking, best, or a pleasant mixture of them all."

ar[10] = "Many that live deserve death. And some die that deserve life. Can you give it to them? Then be not too eager to deal out death in the name of justice, fearing for your own safety. Even the wise cannot see all ends."

ar[11] = "'I wish life was not so short,' he thought. 'Languages take such a time, and so do all the things one wants to know about.'"

ar[12] = "Still round the corner there may wait, a new road or a secret gate."

ar[13] = "There I lay staring upward, while the stars wheeled over... Faint to my ears came the gathered rumour of all lands linenums:0'>var ar = new Array(44)ar[0] = "Do not meddle in the affairs of wizards, for they are subtle and quick to anger."ar[1] = "Faithless is he that says farewell when the road darkens."ar[2] = "I cordially dislike allegory in all its manifestations, and always have done since I grew old and wary enough to detect its presence. "ar[3] = "If more of us valued food and cheer and song above hoarded gold, it would be a merrier world."ar[4] = "It's a job that's never started that takes the longest to finish."ar[5] = "Little by little, one travels far."ar[6] = "I don't know half of you half as well as I should like; and I like less than half of you half as well as you deserve."ar[7] = "It's a dangerous business going out your front door."ar[8] = "All that is gold does not glitter."ar[9] = "His house was perfect, whether you liked food, or sleep, or work, or story-telling, or singing, or just sitting and thinking, best, or a pleasant mixture of them all."ar[10] = "Many that live deserve death. And some die that deserve life. Can you give it to them? Then be not too eager to deal out death in the name of justice, fearing for your own safety. Even the wise cannot see all ends."ar[11] = "'I wish life was not so short,' he thought. 'Languages take such a time, and so do all the things one wants to know about.'"ar[12] = "Still round the corner there may wait, a new road or a secret gate."ar[13] = "There I lay staring upward, while the stars wheeled over... Faint to my ears came the gathered rumour of all lands: the springing and the dying, the song and the weeping, and the slow everlasting groan of overburdened stone."ar[14] = "Courage is found in unlikely places."ar[15] = "Advice is a dangerous gift, even from the wise to the wise."ar[16] = "The world changes, and all that once was strong now proves unsure."ar[17] = "All we have to decide is what to do with the time that is given to us."ar[18] = "The world is indeed full of peril, and in it there are many dark places; but still there is much that is fair; and though in all lands love is now mingled with grief, it grows perhaps the greater."ar[19] = "But I expect they had lots of chances, like us, of turning back, only they didn't. And if they had, we shouldn't know, because they'd have been forgotten. We hear about those as just went on -- and not all to a good end, mind you; at least not to what folk inside a story and not outside it call a good end."ar[20] = "The board is set, the pieces are moving. We come to it at last... The great battle of our time."ar[21] = "The Darkness has begun. There will be no dawn."ar[22] = "Their plans were improved with the best advice."ar[23] = "With hope or without hope we will follow the trail of our enemies. And woe to them, if we prove the swifter!"ar[24] = "Their Oath shall drive them, and yet betray them, and ever snatch away the very treasures that they have sworn to pursue."ar[25] = "The treacherous are ever distrustful."ar[26] = "He should not vow to walk in the dark, who has not seen the nightfall."ar[27] = "Few can foresee whither their road will lead them, till they come to its end."ar[28] = "It's wisdom to recognize necessity, when all other courses have been weighed, though as folly it may appear to those who cling to false hope."ar[29] = "For though I do not ask for aid, we need it."ar[30] = "I have no help to send, therefore I must go myself."ar[31] = "What do you mean? Do you wish me a good morning, or mean that it is a good morning whether I want it or not; or that you feel good on this morning; or that it is a morning to be good on?"ar[32] = "He that breaks a thing to find out what it is has left the path of wisdom."ar[33] = "The deeds will not be less valiant because they are unpraised."ar[34] = "I sit beside the fire and think of all that I have seen."ar[35] = "I wish I had never come here, and I don't want to see no more magic."ar[36] = "The Road goes ever on and on, down from the door where it began."ar[37] = "It all comes from here, the stench and the peril."ar[38] = "But only a short way ahead its floor and the walls on either side were cloven by a great fissure, out of which the red glare came, now leaping up, now dying down into darkness; and all the while far below there was a rumour and a trouble as of great engines throbbing and labouring."ar[39] = "A box without hinges, key, or lid, yet golden treasure inside is hid."ar[40] = "Go not to the elves for counsel, for they will say both yes and no."ar[41] = "I am told that I talk in shorthand and then smudge it."ar[42] = "It does not do to leave a live dragon out of your calculations, if you live near him."ar[43] = "Not all who wander are lost."var now = new Date()var sec = now.getSeconds()document.write("<table cellspacing='0' cellpadding='0' border='0'>")document.write("<tr><td><img src='img/scroll_top.gif' width=160 height=37 border=0 alt=''></td></tr>")document.write("<tr><td background='img/scroll_mid.gif'><table align='center' cellspacing='5' cellpadding='5' border='0'><tr>")document.write("<td valign='top'><font size=-1><i>" + ar[sec % 44] +"</i></font><font size=-1 color=#800000><p align='right'><b>J.R.R. Tolkien</b></p></font></td></tr></table>")document.write("</td></tr><tr><td><img src='img/scroll_bot.gif' width=161 height=27 border=0 alt=''></td></tr></table>")

The variables you need to set are the top line "var ar = new Array(44)" 44 is the number of quotes, in this case we start at 0 and go to 43, that's 44 lines. Then of course you need set each of the quote variables "ar[0], ar[1], ar[2]" etc to your quotes.

 

The "document write" statements format the text and display it on the page. You will want to change the images used in the table to what ever images you want to use.

 

Copy and paste the code above into a text editor, make your changes and save the file as "quote.js" and then upload it to the server.

 

To call the script you just need to put this code in your page wherever you want the quotes to appear.

<script src="quote.js"></script>
This assumes the script is in the the same directory as the page calling the script.

 

An example of the above script can be seen here. Look in the bottom left corner of the page, reload the page to see a new quote.

 

Have fun with it! ;)

 

Notice from cmatcmextra:
Use [-codebox-] tags when writing long lines of code! This is script - it does not discuss any website - moving out of Website Discussion --> General

Notice from KuBi:
On top of cmats note, warning issued.
Edited by KuBi (see edit history)

Share this post


Link to post
Share on other sites

A very nice script but I have a one question and a suggestion:

What happens when the amount of seconds is above 43, does it call ar[0] or display nothing or just use ar[43]?

Wouldn't it be easier to use and better (if I wanted to use more than 60 quotes) to use javascript's Math.random() instead now.getSeconds():

 

var sec = Math.round(Math.random()*43);

Where 43 is the amount of array number things used. You can replace 43 with any sized number you like.


Share this post


Link to post
Share on other sites

This can be done easier with php.

<?$quote = array("quote1","quote2","quote3","quote4","quote5");$rand = array_rand($quote, 1);echo "$quote[$rand]";?>

Share this post


Link to post
Share on other sites

I like truefusion's php solution better, it's a lot simpler. I'll use that in the future. All you'd need to do is save it as a php page by itself and then use an 'include' statement wherever you want it on the other page. Good work!I guess the javascript is handy for when you don't have access to php, but that's not a problem at Xisto. ;)

Share this post


Link to post
Share on other sites

I agree also, nice work bro with the JavaScript. I haven't been such an avid javascript user, so you're cool in my eyes for that.But yeah, the PHP version is quicker, more reliable, and easier. Glad to see that we can accept superiority when it presents itself ;)Just messin, nice work both of you.Mr. Panda

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.