blueskyboris
02-03-2005, 08:25 PM
Hi there,
I am looking for java code that will allow me to display random quotations on my website. Does anyone know of a code that will work?
Thanks.
the_pm
02-03-2005, 08:41 PM
Not in Java. I have a good script written in JavaScript that does the job well. Does it have to be Java, or will JavaScript work? They both have some reliability issues, but it's pretty easy to gracefully degrade a random rotation JavaScript. Not so sure about Java.
blueskyboris
02-03-2005, 08:50 PM
I need something that will work. If javascript will do the job then javascript will do the job. I want to be able to display a limitless amount of random quotes.
the_pm
02-03-2005, 09:20 PM
Code in your .js file
// Don't forget to increment the array number for each new quote
quote = new Array();
quote[0] = "I'll be back."
quote[1] = "Make my day."
quote[2] = "Carpe Diem."
function RandElement(IList)
{
return(IList[Math.round(Math.random()*(IList.length-1))]);
}
Code in you HTML document:
<script type="text/javascript">document.write(RandElement(picture));</script>
<noscript>Fallback quote in case JavaScript is disabled goes here.</noscript>
Easy :)
blueskyboris
02-03-2005, 09:37 PM
does it matter what .js file it goes in?
(Thank you)
blueskyboris
02-03-2005, 10:17 PM
hmm,
I am working off a free server type rig. I made a .js file for the first bit f code, but i do not know how to made so the second bit of code reads the first bit.
the_pm
02-03-2005, 10:18 PM
Nope, just as long as it's linked to the page where you want your quotes to be. And make sure you don't accidently stick it inside another function, of course :)
Other than that, it should work fine. You can also add the JavaScript into the page head area in <script type="text/javascript"> tags.
(you're welcome)
blueskyboris
02-04-2005, 04:26 PM
I have employed your code, but with no success. Take a look at the page source (the code is near the page counter code). I set up the quote code in a quote.js file.
the_pm
02-04-2005, 04:41 PM
Sorry. I pulled the script from another site I'd made, and I forgot to make one change. Use this in your HTML document instead:
<script type="text/javascript">document.write(RandElement(quote));</script>
Also, you didn't link to the external .js file from the HTML document. Sorry, I assumed you'd already set this part up. You'll need to put the following in the page's <head> section:
<script type="text/javascript" src="quote.js"></script>
for the src attribute, you'll need to make sure the path to the file is correct. If it's in the same folder as the HTML page, you won't need to adjust anything, just drop it is.
This script will display the quote wherever you drop it, so if you put it last in your HTML document, it will be at hte bottom of the page.
Also, I just looked at quote.js, and it's code for an HTML document. It should only contain the code I gave you above, nothing else.
blueskyboris
02-04-2005, 05:01 PM
still not working :confused:
hmm, maybe my webhost doesn't allow javascript or something?
I really do not understand your html in the js file statement. When i look there is only javascript.
peynir
02-05-2005, 08:25 AM
Have u get the job done yet? I think you could try do this in PHP and a txt file.
the_pm
02-05-2005, 01:22 PM
Here, I'll make this even easier. Here's all of the code, fully functional. Pick out the pieces and stick them into your page.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Random Quote Generator</title>
<script type="text/javascript">
// Don't forget to increment the array number for each new quote
quote = new Array();
quote[0] = "I'll be back."
quote[1] = "Make my day."
quote[2] = "Carpe Diem."
quote[3] = "Make him an offer he can't refuse."
quote[4] = "Bond, James Bond."
quote[5] = "Frankly Scarlet, I don't give a damn."
function RandElement(IList)
{
return(IList[Math.round(Math.random()*(IList.length-1))]);
}
</script>
</head>
<body>
<p style="font-weight:bold"><script type="text/javascript">document.write(RandElement(quote));</script>
<noscript>Fallback quote in case JavaScript is disabled goes here.</noscript></p>
<p><a href="javascript:window.location=window.location">Refresh to change quote</a></p>
</body>
</html>
Ignore the link that refreshes the page. I just put that in there so you could see how the quote changes every time you refresh the page (you can also just hit the refresh on your browser to see it).
Take the code above and save it as a .html page. You'll see how it all works.
blueskyboris
02-05-2005, 11:32 PM
I got it to work.
Thanks for your help.