Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder DR.Wong's Avatar
    Join Date
    Jan 2005
    Posts
    360
    Thanks
    23
    Thanked 1 Time in 1 Post

    Question Case statment anyone?

    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 :

    if
    date="1/07/2005"
    then document.write("Today is famous because...);
    else...

    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!!!
    (The guy who know this is the man!)
    -DR.Wong

    Wheres the food at?

  • #2
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    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!!

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/

  • #3
    Regular Coder DR.Wong's Avatar
    Join Date
    Jan 2005
    Posts
    360
    Thanks
    23
    Thanked 1 Time in 1 Post

    Thumbs up

    Okay, I dont know if you saw the posts about the project. This is all client side.

    But thanks anyway!
    -DR.Wong

    Wheres the food at?

  • #4
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    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.

    You got how to get today's date, right?
    Here's a little tutorial on working with them.
    http://www.w3schools.com/js/js_obj_date.asp

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/

  • #5
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    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.

    PHP Code:
    <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()
    nowMth=now.getMonth()+1
    nowDay
    =now.getDate()

    function 
    test(){
    document.getElementById("div1").innerHTML=""
    for(i=0;i<theEvent.length;i++){
    temp=theEvent[i][0].split("/")

    if(
    temp[0]==nowDay&&temp[1]==nowMth){
    document.getElementById("div1").innerHTML+="In "+temp[2]+" "+theEvent[i][1]+"<br>"
    }

    }

    }
    //-->
    </script>

    <a href="#null" onclick="test()">Get me</a>
    <div id="div1"></div> 
    Last edited by Mr J; 07-02-2005 at 11:17 PM.
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #6
    Regular Coder DR.Wong's Avatar
    Join Date
    Jan 2005
    Posts
    360
    Thanks
    23
    Thanked 1 Time in 1 Post

    Question

    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!!!!!
    -DR.Wong

    Wheres the food at?

  • #7
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Just do away with the link and put

    onload="test()"

    in the opening BODY tag.


    You could also change the function name, test, to something more meaningful
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #8
    Regular Coder DR.Wong's Avatar
    Join Date
    Jan 2005
    Posts
    360
    Thanks
    23
    Thanked 1 Time in 1 Post

    Thumbs up

    Thanks alot Mr J!
    -DR.Wong

    Wheres the food at?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •