Today is?
tested with
+
+
JavaScript (a.k.a LiveScript or more recently
ECMAScript) deals mostly with
HTML functions and most of these are not included by the VRML browser manufactures. The typical
VRML browser uses only the basic syntax and a few extra utilities like string manipulation, the 'Math' functions,
and the 'Date' object. Today we write a few lines of code which will provide your world-visitor with
the accurate date each time s/he loads your world (ja, ja, I know, it's not really super-useful ...
but educational!).
We start with creating a new 'Date' object during 'initialize()':
newDate = new Date();
This will give us something like: 'December 24, 1999, 12:00:00' which is fantastic but not what we want.
Two more function calls give us the day of the week and the day of the month:
day = newDate.getDay();
date = newDate.getDate();
While 'getDate();' is well-behaved and returns '24', 'getDay()' returns '0'. Searching for this function
in the JavaScript Specification
reveals that '0' means 'Sunday', '1' means 'Monday' and so on. Our Script needs to
translate that:
if(day == 0) dayString = 'Sunday';
else if(day == 1) dayString = 'Monday';
else if(day == 2) dayString = 'Tuesday';
else if(day == 3) dayString = 'Wednesday';
else if(day == 4) dayString = 'Thursday';
else if(day == 5) dayString = 'Friday';
else if(day == 6) dayString = 'Saturday';
'dayString' now holds the correct day of the week. The day and the date can now be concatenated and
displayed:
Shape {
geometry DEF T Text { string [ ] }
}
Script {
directOutput TRUE
field SFNode T USE T
url "javascript:
function initialize() {
newDate = new Date();
day = newDate.getDay();
date = newDate.getDate();
if(day == 0) dayString = 'Sunday';
else if(day == 1) dayString = 'Monday';
else if(day == 2) dayString = 'Tuesday';
else if(day == 3) dayString = 'Wednesday';
else if(day == 4) dayString = 'Thursday';
else if(day == 5) dayString = 'Friday';
else if(day == 6) dayString = 'Saturday';
textString = new MFString();
textString[0] = 'Today is '+dayString+' the '+date;
T.set_string = textString;
}
"
}
A Text node has an exposedField 'string' which happens to be of type MFString. In
order to set the string we need to create a MFString object: 'textString'. The JavaScript string is
assigned to the first SFString: 'textString[0] = 'javascriptstring';' The MFString 'textString'
is then used in the EventIn call 'set_string' on an empty text node (
todayis source and
todayis.wrl).
To make the whole thing a little bit more nice looking you can add some enhancements to the date:
if(date == 1 || date == 21 || date == 31) date = date + 'st';
else if(date == 2 || date == 22) date = date + 'nd';
else if(date == 3 || date == 23) date = date + 'rd';
else date = date + 'th';
If date is either '1' or '21' or '31' then you add 'st' to the date. The rest as usual (
todayis source and
todayis.wrl).
JavaScript shows itself here from its userfriendly side: without any headache you can assemble a string
from integers and other strings via the '+' sign.

Copyright © 1996-98
Markus Roskothen. All rights reserved.