View Full Version : Case statment anyone?

07-01-2005, 01:24 PM
Hi guys,

Yes, its another question in hopes to forever imprvoe my "space education for the kids" project.

What I now want to add is a "This day in histroy" function. As soon as they hit the index of the little offline website, I want to display a piece of text that says what happened in space history on that day.

I know I have to use 2 things:
1) a case statement
2) a document.write statement

The problem is, I dont exactly know how to pull the date off of the users PC and then say :

then document.write("Today is famous because...);

If you know how to do this, please wont you drop off an example. I am not asking for a whole This day in history script,
I just need like some random words on the first few days of the year.

If you can refer me to a site, please do...

Thanks a heck of alot!!! :cool: :)
(The guy who know this is the man!)

07-01-2005, 10:45 PM
Google javascript dates. Easy as pie to get today's date with a simple new Date().
Don't go with a case statement. There are 365 days in a year!

Document.write can have some interesting side effects. You may want to reconsider using it and opt for DOM methods or innerHTML instead.

I would totally do this with either a database and server-side stuff, or 365 small text/javascript files with a specific file naming format and include them in dynamically based on today's date.
Anything but 365 case or if/else statements!! :D

07-02-2005, 01:55 PM
Okay, I dont know if you saw the posts about the project. This is all client side.

But thanks anyway! :cool:

07-02-2005, 02:53 PM
I might have, but I was just looking in this thread and missed where you said "offline". My bad.
Still, that's a huge bear to maintain. Use a 2-dim ("associative") array for storing and DOM methods or innerHTML to change elements. Save yourself some headaches later.
My 2 cents, anyway. :D

You got how to get today's date, right?
Here's a little tutorial on working with them.

Mr J
07-02-2005, 11:14 PM
I would imagine that there are quite a few ways to tackle this but here's a quick draft to see if it's on the right lines.

Depending on when you read this post you might have to change the day from 2 to the current day in the array to see the example work.

<script type="text/javascript">
theEvent=new Array()
theEvent[0]=new Array("2/7/2005","Something happened")
theEvent[1]=new Array("2/7/2004","An event took place")
theEvent[2]=new Array("2/7/2003","Something really big")
theEvent[3]=new Array("2/7/1999","Burnt the toast")

now=new Date()

function test(){

document.getElementById("div1").innerHTML+="In "+temp[2]+" "+theEvent[i][1]+"<br>"



<a href="#null" onclick="test()">Get me</a>
<div id="div1"></div>

07-03-2005, 03:10 PM
Mr J,
You are officially the man. This is perfect.

One thing I may want to ask is; is it possible to display it automatically?

This is great!
Thanks alot!!!!! :cool: :)

Mr J
07-03-2005, 10:12 PM
Just do away with the link and put


in the opening BODY tag.

You could also change the function name, test, to something more meaningful

07-05-2005, 09:15 AM
Thanks alot Mr J! :thumbsup: