I know diddly shit about JavaScript, so it was probably foolhardy of me to even attempt this. But I saw a homepage that had an engine to pop up a quotation at random, and I said, I want that! So I ripped it off and tried to adapt it for my page. Here it is:
<script language="JavaScript">
<!--array by whiz!
quotes = new Array()
quotes[0] = "'So I'm finally going mad,' she thought, 'like everybody else. What a nuisance. Though I suppose it will be a change.'<br><font size = 1>Margaret Atwood, </i>The Edible Woman<i>"
quotes[1] = "Gracious me. Was I raving? Please forgive me. I'm mad.<br><font size = 1>Patient X, </i>The Exorcist III<i>"
<!-- etc, etc, etc, etc -->
quotes[118] = "Tour à tour on me chasse de vos fréquentations <br>Je n'admet qu'on menace mes résolutions <br>Je me fous bien des qu'en-dira-t-on <br>Je suis chaméléon... <br><font size = 1>Mylène Farmer, 'Sans contrefaçon' "
now = new Date()
seconds = now.getSeconds()
choice= (seconds % 119)
document.write (quotes[choice])
// -->
</script>
Now, given my (marginal) knowledge of programming, apparently what this does is to take the clock time in seconds and use that to derive the number in order to choose a semirandom quotation. My problem is, there being 60 seconds in a minute, it only ever uses the first 60 quotations, never the last ones.
Is there a way I could get it to choose in a similar way from a pool of, say, 120?
How about this:
<!--Pseudocode-->
minuteParity = clockMinute mod 2
<!-- returns 1 if minute odd, 0 if even>
second = clockSecond + (minuteParity * 60)
<!-- etc, as above -->
Could I do this in Java? If so, will you please tell me how?
Java != JavaScript. The names are confusing, but they are not the same language. CnoteChris, the very first link in your link was to Is JavaScript same as Java? to which the answer is no. For a brief description of the two languages, this is a good link.
Matt, I’ve never used JavaScript, but the logic you have put forth as to why you only ever see the first 60 quotes and how to fix it using minutes % 2 seems sound to me. Have you tried it? This isn’t like a chemistry set, you can’t accidentally blow yourself up by screwing up your JavaScript. Well, unless you work for NORAD.
Sailor, if seconds is always less than 60, then seconds % 119 is always less than 60. Matt, have you experimentally verified you only get the first 60 quotes? if ‘getSeconds’ returns the number of seconds total in the day then you’d be OK, but if it returns only the seconds portion of the current time you are screwed as you suggest.
Kyberneticist’s approach, if such functions exist in JavaScript, should also work.
I say go for it, and let us know if it worked.
Since you also asked how, I’m going to do a bit of whacking and hopefully it will help:
now = new Date()
seconds = now.getSeconds()
minutes = now.getMinutes()
choice= (seconds + (minutes % 2) * 60)
document.write (quotes[choice])
If I got any errors with this, I’d start with a javascript tutorial page to track down the exact way of dealing with current time.
Yeah, the simplest way is to simply generate a random number between 1 and how many elements are in your array.
Use the math object as shown above.
But may I offer that this whole thing sucks. You’re forcing people to download a gigantic list of quotes, just so you can spew one at random on the screen.
Let me refer you to the best page on web design on the internet: http://www.useit.com
Well, if he doesn’t care about his server, he could start using SSI, maybe an exec of fortune, or an equivelant quote spewing program.
But in my opinion, if the quotes are all as short as the ones he showed here, where the longest one is around 200 characters…
120 200 * =
24000
A pitiful 23k download of text will not take long with modern 56 k modems, and compression algorithms.
Granted, I’m currently dialed up at 4800 due to connection problems…
Once I start thinking about something like this…
How about PHP and MySQL? Easy to both insert quotes, and grab them in short periods of time. MySQL even has a random function…
23K is a LOT of uneccessary download. Current professional standards for web pages suggest that the entire page should not be more than 50K in length. Using up half of it for a quote-of-the-day is simply bad design.
There has been a ton of stuff written on usability and web design. The link I posted is an excellent start. And all of it suggests that page length is much, much more important than your brush-off would suggest. I work as a professional internet/intranet developer, and any page submitted by one of our graphic designers that goes over 50K goes straight back to him for re-design or at least an explanation of why the size was necessary. Our main corporate web site has several thousand pages on it, and of all of those, perhaps a dozen go over that size limit.
Back to the technical stuff: I had assumed that this has to be a client-only solution. If you can write a server script, the solution is a trivial exercise. You don’t need MySQL, or Access, or anything else. Just create a CGI object or an ASP application variable with the array hard-coded, and fire the right one out to the client. A database driven solution is fine if the list is constantly changing or will be much larger than the 23K list presented. But as this sounds like a home page probably being hosted somewhere like Geocities or something, server-side solutions are pretty much out.
does that 50K include all graphics? or just the text? If it’s just the HTML then I agree but a couple of photos will put you over 50K no matter what.
I do agree though that those pages that take forever to download are an abomination. I do not have the patience to wait for some dumb javascript that takes an eternity to download so you can see some dumb visual effect.
I try to design the page simple and fast to download. I make a point of compressing photos to the limit, even if quality suffers a bit. Then they can click on the photo for a better quality version of it.
Kyber, I used your randomization procedure and it works. However, it seems to return certain quotations with more frequency than others (after hitting “reload” a couple of hundred times, I noticed that I kept seeing certain ones and not others.) Can you think why this might be?
Matt, what they are saying is that you are forcing people to download all of your quotes, then only showing them one. This is poor etiquette, but I think this is a forgivable offense. For more examples of how to make bad web pages, see http://www.webpagesthatsuck.com
The alternative to ‘doing it client-side’ (translation: giving them a JavaScript and all the quotes and forcing their computer to run a program to pick a quote) is to do it on the server side (translation: making the web server pick a quote and construct the web page before sending it.) PHP (with or without MySQL), CGI, ASP, all these things are server related technologies. You may not have access to the server in the way needed to do anything fancy - you may be able to run a CGI program, but not even that is guaranteed. So, none of this matters if you don’t know how to do it or possibly can’t get permission.
What I’m trying to say is you don’t need to know what all these things are. The main thing is that in order to use them you a) have to know what you are doing and b) have to have the permission of the people running the server. In order to just slap a page up that anyone can use without bugging the Powers That Be at your service provider, JavaScript is the quickest.
Good luck, and don’t PQT on your UIF, or you may end up with KJL.
Sailor: I meant the whole page, including graphics. You’d be surprised how much you can optimize a photo.
Check out http://www.msn.com. That whole page’s graphics are 21K. That’s all of them. The length of the HTML itself varies depending on the options you have set, from somewhere around 30K to about 80K. And that’s a BIG page.
Nope, should return a random integer between 0 and 119…
Try tossing it into a loop that prints it out 30 or 40 times, see if it’s your imagination, or if certain numbers seem to show up more.
Also, some browsers cache processed javascript, so in some, it may just keep displaying the same result until it decides to check again…