Click to See Complete Forum and Search --> : today's date activated message script
puppi
10-28-2003, 03:36 PM
I want a message to appear on screen that is appropriate to the date the user is viewing. I think this should work, but it doesn't. Any hints?
//declare the variable date
var date=(getMonth()+1)+"/"(getDate);
switch (date)
{
case "1/4":
document.write("Today is Mary's birthday.");
break;
case "10/26":
document.write("Today is Laura's birthday.");
break;
case "12/13":
document.write("Today is Larry and Susan's wedding anniversary.");
case "12/27":
document.write("Today is Connor's birthday.");
break;
default:
document.write("Nothing of significance today.");
break;
}
fredmv
10-28-2003, 03:55 PM
Welcome to the forums. :cool:
Your main problem is that you aren't accessing those values from a Date object. The methods getMonth and getDate are methods of the Date object, therefore you need to create a new instance of it and call the methods from it. I also fixed another problem within your switch statement, you forgot a break after one of the cases. I also made it so based on the date, it changes the value of a variable. I felt this made more sense so you don't have to keep calling the write method over and over. Now, it assigns the msg variable a different message based on the current date, then writes out the value of that variable. Here's the final source code:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>untitled</title>
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=iso-8859-1" />
<script type="text/javascript">
//<![CDATA[
var now = new Date();
var date = ( now.getMonth() + 1 ) + "/" + ( now.getDate() );
var msg = "Nothing of significance today.";
switch( date )
{
case "1/4":
msg = "Today is Mary's birthday.";
break;
case "10/26":
msg = "Today is Laura's birthday.";
break;
case "12/13":
msg = "Today is Larry and Susan's wedding anniversary.";
break;
case "12/27":
msg = "Today is Connor's birthday.";
break;
default:
break;
}
//]]>
</script>
</head>
<body>
<div>
<script type="text/javascript">
//<![CDATA[
document.write( msg );
//]]>
</script>
</div>
</body>
</html>I hope that helps you out.
puppi
10-28-2003, 07:31 PM
Thank you so much for your assistance, Fred. You have no idea how long I have been laboring over this since I am just learning JavaScript. In our family genealogy page, I hope to have birthdays, anniversaries, as well as family history appear on appropriate dates. Thanks to you - this now works!
fredmv
10-28-2003, 08:43 PM
You're very welcome. JavaScript is a great language to learn, and when starting, simple mistakes can be very hard to find. If you ever need anymore help, please feel free to ask. :)