PDA

View Full Version : How to make this script display the events for the whole year?



terkini
08-20-2006, 04:21 PM
Hi guys,
I use the script below to display on our college's intranet a birthday greetings or event for that day. My problem is that the script can only be used for one particular month only. I have to change/update it for next month.
My problem is that, sometime i forget to change/update the birthday list for the next month and the script will "recycle" the list and display the birthday greetings of the previous month. And this is very embarrasing.
Is there any way that i can modify the script to make it display the birthday greeting and events for the whole year. This will definitely save me a lot of time and embarrasment.
Thank you.

<SCRIPT LANGUAGE="JavaScript">
<!--

var ar = new Array(".",
"Happy Birthday to John",
".",
".",
".",
"Happy 10th Anniversary to Max and Jill",
".",
".",
".",
".",
".",
"Happy Birthday to Trisha",
".",
".",
".",
".",
".",
".",
".",
".",
".",
"Happy brithday to Hannah",
".",
".",
".",
"Happy Birthday to Miriam",
".",
".",
".",
"Tomorrow is the last day to submit your assignment.",
".");

var now = new Date();
var num = now.getDate() - 1;
num %= ar.length;
document.write(ar[num]);

// -->
</SCRIPT>

Twey
08-20-2006, 07:21 PM
Hm, that's a bit ugly, isn't it? Try this instead:
<script type="text/javascript">
String.prototype.isDMString = function() {
var a;
return (
/^\d{2}\/\d{2}$/.test(this) &&
(a = this.match(/d{2}/))[0] * a[1] > 0 &&
a[0] <= 31 &&
a[1] <= 12
);
};

Number.prototype.pad = function(len) {
var n = this.toString();
while(n.length < len)
n = "0" + n;
return n;
};

function Calendar() {
var currDate = "01/01";
for(var i = 0; i < arguments.length; ++i)
if(arguments[i].isDMString()) {
currDate = arguments[i];
if(!this[currDate]) this[currDate] = [];
} else this[currDate].push(arguments[i]);
}

Calendar.prototype.addEvent = function(date, event) {
if(typeof date !== 'string' || !date.isDMString())
return false;
if(!this[date]) this[date] = [];
this[date].push(event);
return true;
};

Calendar.prototype.getTodaysEvents = function() {
var now;
return this[(now = new Date()).getDate() + (now.getMonth() + 1).pad(2)];
};

var cal = new Calendar(
"02/01", "Happy Birthday to John",
"06/01", "Happy Birthday to Max", "Happy Birthday to Jill",
"12/01", "Happy Birthday to Trisha",
"22/01", "Happy Birthday to Hannah",
"26/01", "Happy Birthday to Miriam",
"30/01", "Tomorrow is the last day to submit your assignment."
);

document.write(cal.getTodaysEvents().join("<br>"));
</script>Untested.

terkini
08-21-2006, 02:07 AM
Hm, that's a bit ugly, isn't it? Try this instead:
<script type="text/javascript">
String.prototype.isDMString = function() {
var a;
return (
/^\d{2}\/\d{2}$/.test(this) &&
(a = this.match(/d{2}/))[0] * a[1] > 0 &&
a[0] <= 31 &&
a[1] <= 12
);
};

Number.prototype.pad = function(len) {
var n = this.toString();
while(n.length < len)
n = "0" + n;
return n;
};

function Calendar() {
var currDate = "01/01";
for(var i = 0; i < arguments.length; ++i)
if(arguments[i].isDMString()) {
currDate = arguments[i];
if(!this[currDate]) this[currDate] = [];
} else this[currDate].push(arguments[i]);
}

Calendar.prototype.addEvent = function(date, event) {
if(typeof date !== 'string' || !date.isDMString())
return false;
if(!this[date]) this[date] = [];
this[date].push(event);
return true;
};

Calendar.prototype.getTodaysEvents = function() {
var now;
return this[(now = new Date()).getDate() + (now.getMonth() + 1).pad(2)];
};

var cal = new Calendar(
"02/01", "Happy Birthday to John",
"06/01", "Happy Birthday to Max", "Happy Birthday to Jill",
"12/01", "Happy Birthday to Trisha",
"22/01", "Happy Birthday to Hannah",
"26/01", "Happy Birthday to Miriam",
"30/01", "Tomorrow is the last day to submit your assignment."
);

document.write(cal.getTodaysEvents().join("<br>"));
</script>Untested.

Professor Twey,
Thanks for your great effort in re-writing the codes. Unfortunately, when i run it, nothing appeared on the screen. For your information, I have also modified the dates to August so that my PC can displayed it, but still nothing appeared on the screen. Another thing that I would like the script to do is to enable me to decide the color and type of fonts used in the birthday greeting. For the guys, I want to use Times Roman in black and for girls, I want to use tahoma or arial in blue. I really hope somebody can help me on this.

Here are the javascripts codes that i tried to run:

<script type="text/javascript">
String.prototype.isDMString = function() {
var a;
return (
/^\d{2}\/\d{2}$/.test(this) &&
(a = this.match(/d{2}/))[0] * a[1] > 0 &&
a[0] <= 31 &&
a[1] <= 12
);
};

Number.prototype.pad = function(len) {
var n = this.toString();
while(n.length < len)
n = "0" + n;
return n;
};

function Calendar() {
var currDate = "01/01";
for(var i = 0; i < arguments.length; ++i)
if(arguments[i].isDMString()) {
currDate = arguments[i];
if(!this[currDate]) this[currDate] = [];
} else this[currDate].push(arguments[i]);
}

Calendar.prototype.addEvent = function(date, event) {
if(typeof date !== 'string' || !date.isDMString())
return false;
if(!this[date]) this[date] = [];
this[date].push(event);
return true;
};

Calendar.prototype.getTodaysEvents = function() {
var now;
return this[(now = new Date()).getDate() + (now.getMonth() + 1).pad(2)];
};

var cal = new Calendar(
"19/08", "Happy Birthday to John",
"20/08", "Happy Birthday to Max", "Happy Birthday to Jill",
"21/08", "Happy Birthday to Trisha",
"22/08", "Happy Birthday to Hannah",
"23/08", "Happy Birthday to Miriam",
"24/08", "Tomorrow is the last day to submit your assignment."
);

document.write(cal.getTodaysEvents().join("<br>"));
</script>

Twey
08-21-2006, 03:17 AM
I did warn you it was untested :) This is the one that works:
<script type="text/javascript">
String.prototype.isDMString = function() {
var a;
return (
/^\d{2}\/\d{2}$/.test(this) &&
(a = this.match(/(\d{2})\/(\d{2})/))[1] * a[2] > 0 &&
a[1] <= 31 &&
a[2] <= 12
);
};

Number.prototype.pad = function(len) {
var n = this.toString();
while(n.length < len)
n = "0" + n;
return n;
};

function Calendar() {
if(!arguments[0].isDMString) return;
for(var i = 0; i < arguments.length; ++i)
if(arguments[i].isDMString()) {
currDate = arguments[i];
if(!this[currDate]) this[currDate] = [];
} else {
this.addEvent(currDate, arguments[i]);
}
}

Calendar.prototype.addEvent = function(dte, event) {

if(typeof dte !== 'string' || !dte.isDMString())
return false;
if(!this[dte]) this[dte] = [];
this[dte].push(event);
return true;
};

Calendar.prototype.getTodaysEvents = function() {
var now;
return (this[(now = new Date()).getDate().pad(2) + "/" + (now.getMonth() + 1).pad(2)] || []);
};

Calendar.prototype.toString = function() {
var op = "";
for(var i in this) {
if(!i.isDMString()) continue;
op += "\n" + i + ": " + this[i].join(", ");
}
return op;
};

// NOTE: DD/MM format. I'm a Brit. :-P
var cal = new Calendar(
"02/01", "Happy Birthday to John",
"06/01", "Happy Birthday to Max", "Happy Birthday to Jill",
"12/01", "Happy Birthday to Trisha",
"22/01", "Happy Birthday to Hannah",
"26/01", "Happy Birthday to Miriam",
"30/01", "Tomorrow is the last day to submit your assignment."
);

document.write(cal.getTodaysEvents().join("<br>"));
</script>
Another thing that I would like the script to do is to enable me to decide the color and type of fonts used in the birthday greeting. For the guys, I want to use Times Roman in black and for girls, I want to use tahoma or arial in blue.You can include <span> or (shudder) <font> tags just as you would in plain HTML. Just remember to escape quotes; for example,
<span style="color:blue;">Happy Birthday to John</span>becomes:
"<span style=\"color:blue;\">Happy Birthday to John</span>"

terkini
08-21-2006, 05:34 AM
I did warn you it was untested :) This is the one that works:
<script type="text/javascript">
String.prototype.isDMString = function() {
var a;
return (
/^\d{2}\/\d{2}$/.test(this) &&
(a = this.match(/(\d{2})\/(\d{2})/))[1] * a[2] > 0 &&
a[1] <= 31 &&
a[2] <= 12
);
};

Number.prototype.pad = function(len) {
var n = this.toString();
while(n.length < len)
n = "0" + n;
return n;
};

function Calendar() {
if(!arguments[0].isDMString) return;
for(var i = 0; i < arguments.length; ++i)
if(arguments[i].isDMString()) {
currDate = arguments[i];
if(!this[currDate]) this[currDate] = [];
} else {
this.addEvent(currDate, arguments[i]);
}
}

Calendar.prototype.addEvent = function(dte, event) {

if(typeof dte !== 'string' || !dte.isDMString())
return false;
if(!this[dte]) this[dte] = [];
this[dte].push(event);
return true;
};

Calendar.prototype.getTodaysEvents = function() {
var now;
return (this[(now = new Date()).getDate().pad(2) + "/" + (now.getMonth() + 1).pad(2)] || []);
};

Calendar.prototype.toString = function() {
var op = "";
for(var i in this) {
if(!i.isDMString()) continue;
op += "\n" + i + ": " + this[i].join(", ");
}
return op;
};

// NOTE: DD/MM format. I'm a Brit. :-P
var cal = new Calendar(
"02/01", "Happy Birthday to John",
"06/01", "Happy Birthday to Max", "Happy Birthday to Jill",
"12/01", "Happy Birthday to Trisha",
"22/01", "Happy Birthday to Hannah",
"26/01", "Happy Birthday to Miriam",
"30/01", "Tomorrow is the last day to submit your assignment."
);

document.write(cal.getTodaysEvents().join("<br>"));
</script>You can include <span> or (shudder) <font> tags just as you would in plain HTML. Just remember to escape quotes; for example,
<span style="color:blue;">Happy Birthday to John</span>becomes:
"<span style=\"color:blue;\">Happy Birthday to John</span>"

Wow, you are really a mad but brilliant professor!!! Thanks a lot.
I tried to customised the font type using the guidelines that you give, but i can't get it working.

I tried to make the fonts of the birthday greetings in "blue" and in "arial" using the following codes but it failed to display:

<span style=\"color:blue;\font:arial, tahoma;\">Happy Birthday to Trisha</span>

What's wrong with my script above?

Twey
08-21-2006, 03:44 PM
I think you mean:
<span style=\"color:blue;font-face:arial, tahoma, sans-serif;\">Happy Birthday to Trisha</span>
Wow, you are really a mad but brilliant professor!!!Well, one third right :)

terkini
08-21-2006, 06:29 PM
I think you mean:
<span style=\"color:blue;font-face:arial, tahoma, sans-serif;\">Happy Birthday to Trisha</span>Well, one third right :)
I tried to use the scripts that you give but the birthday greetings were still displayed in Times Roman font, and not arial or tahoma. What could be the problem?

Twey
08-21-2006, 06:34 PM
D'oh. The property is font-family, not font face.
<span style=\"color:blue;font-family:arial, tahoma, sans-serif;\">Happy Birthday to Trisha</span>

mburt
08-21-2006, 06:45 PM
Yeah, I tend to do that too :)

I doesn't make much sense though.. The tag <font>, you'd think would have the same property as in css, in this case "family". But instead, they decide to make it annoying, and change it to "face".

terkini
08-21-2006, 07:01 PM
D'oh. The property is font-family, not font face.
<span style=\"color:blue;font-family:arial, tahoma, sans-serif;\">Happy Birthday to Trisha</span>
The script is working perfectly now. Thanks Prof!!!:)

Twey
08-21-2006, 07:03 PM
I haven't used a <font> tag in years :-\
<Font> tags aside, it's just illogical. A family of fonts is surely a generic grouping, such as serif, sans-serif, monospace or fantasy. That I can understand. But I really don't see how Tahoma can be classified as an entire font family.
The script is working perfectly now. Thanks Prof!!!:)No problem.