today = new Date(); weekdays = new Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"); dow = weekdays[today.getDay()]; window.document.forms["demo"].elements["inday"].value = dow;
In this script fragment, the variable today is set to the current date. An array of all possible weekdays is defined in the variable weekdays. The current day of the week is obtained by calling the getDay() method of the Date object, which returns an integer indicating the day of the week. This value is used to index into the weekdays array to obtain the current day of the week as a string and to assign it to the dow variable. The last line sets the value of a field in a form. The form is named "demo", and the field is an input text control named "inday".
To access the field and to set its value property, the object hierarchy is navigated by beginning with the window object, a browser-supplied global object that more or less represents the browser itself. This object has a property, called the document, which represents the HTML Web page. The document object has a forms collection, since it is possible to define multiple forms in a page. This collection of forms is indexed by the name of the form: "demo". Forms contain form elements, or input fields, that can be accessed by indexing the elements collection of the form object. Once a reference to the field is obtained, its value property is set with the current day of the week.
Figure 3-4. The DOM hierarchy
Overview of Modeling and Web-Related Technologies
Building Web Applications