Click to See Complete Forum and Search --> : Greeting on a person's Birthday
Xeenslayer
05-05-2003, 08:50 AM
I have a list of about 30 to 40 people. I want the page to write out a greeting to that person's name on his or her birthday.
I have used a lot of if and one else command in my JS to get that effect. However, it doesn't seem to work. I tried changing the date to a person's birthday to see the effect, but saw none. Except for the last person in the list, the rest all didn't have their names shown. Instead, on their birthdays, the text "No birthdays today" is shown. What is wrong with my script?
It is here (http://www.dhs3l2003.netfirms.com). Can somebody kindly point out to me my mistake here? I would appreciate it a lot.
Thanks in advance.
requestcode
05-05-2003, 09:15 AM
That is because you have this last if else that will set the message if the month is not December.
if(month == 12 && date == 31) {
bdaywish = "Happy Birthday to Jie Ren!";
}
else {
bdaywish = "There are no birthdays today...";
}
Instead of doing it that way set the value of bdaywish to the "no birthdays" message prior to your if statements and remove it from the else. That way if there are birthdays it will get replaced with the correct message and if there are no birthdays it will not.
khalidali63
05-05-2003, 09:18 AM
oh thats painful to even write that much code..:D
you should do something like this
crete an array with the months and days for everybody in your list
<?xml version="1.0"?>
<!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" lang="en">
<head>
<title>Untitled</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-5"/>
<style type="text/css">
</style>
<script type="text/javascript">
var bdays = new Array(
new Array("Joe 1","04/23"),
new Array("Joe 9","09/21"),
new Array("Jane 3","12/25"),
new Array("Jane 18","05/05")
);
function Process(){
var today = new Date();
var bday = formatDate(today.getMonth()+1)+"/"+formatDate(today.getDate());
//now see if any body's birthday is on this date
var len = bdays.length;
for( var x=0;x<len;x++){
if(bday==bdays[x][1]){
alert("Happy Birthday [ "+bdays[x][0]+" ]");
}
}
}
function formatDate(date){
return (date<10)?("0"+date):date;
}
</script>
</head>
<body onload="Process();">
</body>
</html>
Charles
05-05-2003, 11:45 AM
We can clean that up a bit by using objects.
<script type="text/javascript">
<!--
Date.birthdays = {'5/5': 'Kierkegaard', '5/9': 'Lil ole me'}
Date.prototype.getBirthday = function () {var bd = Date.birthdays[this.getDate() + '/' + (this.getMonth() + 1)]; if (bd) {return bd} else {return 'No birthdays'}};
document.write(new Date().getBirthday());
// -->
</script>