Online Documentation Server
 ПОИСК
ods.com.ua Web
 КАТЕГОРИИ
Home
Programming
Net technology
Unixes
Security
RFC, HOWTO
Web technology
Data bases
Other docs

 


 ПОДПИСКА

 О КОПИРАЙТАХ
Вся предоставленная на этом сервере информация собрана нами из разных источников. Если Вам кажется, что публикация каких-то документов нарушает чьи-либо авторские права, сообщите нам об этом.




Previous Table of Contents Next

You may recall from the previous function, setCal(), that the value passed over to the parameter firstDay is between 1 and 7, not 0 and 6. Therefore 1 is subtracted from firstDay in this expression. Math.ceil((lastDate + firstDay – 1) / 7) represents the minimum number of rows needed in the calendar, or table. The number of cells in the calendar (not including the main header, column headers, and cells after the last day in the month) is lastDate + firstDay – 1, because lastDate is equal to the number of days in the month, and firstDay – 1 is equal to the number of cells before the first date. The value is divided by 7, to get the exact minimum number of rows needed. However, the loop must execute a whole number of times. Therefore, the Math.ceil() method is needed. Other, more simple, calendars just use five rows for every month, no matter what. However, this simple rule of five rows per month fails when (a) the first day of a non-leap year February occurs on Sunday, meaning only four lines are needed, and (b) the first day of a 31-day month is on Friday or Saturday, meaning six rows are needed. Although these situations seldom occur, you must take them into account. If the row computation is replaced by a simple 5 in this script, a month such as February 1987 does not appear properly. Here is a screen shot of this special occurrence, where the script justifiably uses the complex expression (Math.ceil((lastDate + firstDay – 1) / 7)):

Now, here is the output of the script using five rows (notice the width of the bottom border, representing an empty table row):

The nested loop is not nearly as difficult, because it always executes seven times, once for each day of the week. Throughout the entire loop construct, including inner and outer loops, digit holds the current cell to be created. The variable curCell holds the accumulative number of cells in the table created (assigned) thus far. This variable is only needed until the cell of the first day of the month is created, and is not handled afterwards; that is, after the first day of the month, it is not incremented anymore.

The string <TD></TD> is used to create blank cells, used as placeholders. These are only needed before the first day of the month, because the loop is terminated via a break statement after the last day of the month, and the remaining place in the line is filled in the same way as blank cells (placeholders). Each execution of the inner loop creates the current cell of the table. There are basically two types of cells:


Figure 14-9.  A Murphy's law chosen from a list of 20 is displayed.

  • A cell representing the current day, which uses a special font color and displays the time inside the cell
  • All other cells

The HTML tags used are obvious and are not dealt with in depth here.

There are two statements outside the inner loop but inside the containment loop. The first one creates a new table row and the second one ends the current row. Each execution of the inner loop (seven executions or its command block) is responsible for the creation of an entire row, or a partial one if it is the last row and the last day of the month is encountered before the last cell of the row—in this case the loop is terminated via a break statement. The term “create” refers to the concatenation of the proper strings and values and assignment to the variable text.

The last, but definitely not least, statement of the function is the one that actually prints the table to the HTML document. Up to that statement, the HTML document was stored as a string in the variable text.

Random Quotes

The finale for this chapter is a simple, yet interesting script to display a different message each time the page is loaded. Yes, only with JavaScript. Here is the script:

Previous Table of Contents Next


With any suggestions or questions please feel free to contact us