Schongar P.VBScript unleashed.1997
.pdf135:
136:Function makAP_URL(m,d,y)
137:Dim stAppt
138:
139:stAppt = readVariable("%%AP" & m & "/" & d & "/" & y)
140:If Len(Trim(stAppt)) > 0 AND Trim(stAppt) <> "NULL" Then
141:sTemp = "<A LANGUAGE=VBScript HREF=#null onClick="
142:sTemp = sTemp & Chr(34)
143:sTemp = sTemp & "Call addAppt(" & m & ", " & d & ", " & y &
")"
144:sTemp = sTemp & Chr(34)
145:sTemp = sTemp & " onMouseOver="
146:sTemp = sTemp & Chr(34)
147:sTemp = sTemp & "Call ApptStat(" & m & ", " & d & ", " & y & ")" & Chr(34)
&">"
148:sTemp = sTemp & d & "!</a>"
149:Else
150:sTemp = "<A LANGUAGE=VBScript HREF=#null onClick="
151:sTemp = sTemp & Chr(34)
152:sTemp = sTemp & "Call addAppt(" & m & ", " & d & ", " & y &
")"
153:sTemp = sTemp & Chr(34)
154:sTemp = sTemp & " onMouseOver="
155:sTemp = sTemp & Chr(34)
156:sTemp = sTemp & "Call ApptStat(" & m & ", " & d & ", " & y & ")" & Chr(34)
&">"
157:sTemp = sTemp & d & "</A>"
158:End If
159:makAP_URL = sTemp
160: End Function
161:
162:Sub docCreate
163:Dim sDocument
164:sDocument = "<TD ALIGN=LEFT VALIGN=TOP WIDTH=35%><CENTER><B>To Do</B></
CENTER>"
165:sDocument = sDocument & Chr(13) & Chr(10) & "<HR>"
166:For i = 1 to 10
167:sDocument = sDocument & "<BR>" & makTD_URL(i)
168:Next
169:sDocument = sDocument & "</TD><TD ALIGN=CENTER VALIGN=MIDDLE WIDTH=30%>"
170:sDocument = sDocument & makCalendar()
171:sDocument = sDocument & "</TD><TD ALIGN=LEFT VALIGN=TOP WIDTH=35%>"
172:sDocument = sDocument & "<CENTER><B>ADDRESSES</B></CENTER>"
173:sDocument = sDocument & Chr(13) & Chr(10) & "<HR>"
174:For j = 1 to 10
175:sDocument = sDocument & "<BR>" & makAD_URL(j)
176:Next
177:
178:sDocument = sDocument & "</TD></TR>"
179:sDocument = sDocument & Chr(13) & Chr(10) & "</TABLE>"
181:Document.Write sDocument
183:End Sub
185:Function makCalendar()
186:m=Month(Now())
187: y=Year(Now())
188:
189:FirstDay = WeekDay(Dateserial(y,m,1))
190:DaysInMonth= Day(Dateserial(y,m+1,1)-1)
191:ThisMonth=GMonth(Month(Dateserial(y,m,1)), true) & " " &
Year(Dateserial(y,m,1))
192:
193:sCalendar=""
194:sCalendar=sCalendar & "<CENTER><TABLE BORDER=1 CELLPADDING=2>"
195:sCalendar=sCalendar & "<TR><TH COLSPAN=7>" & ThisMonth & "</TH>"
196:sCalendar=sCalendar & "<TR><TH WIDTH=50 ALIGN=CENTER>Sun</ TH><TH WIDTH=50
ALIGN=CENTER>Mon</TH><TH WIDTH=50 ALIGN=CENTER>Tue</
TH><TH WIDTH=50
ALIGN=CENTER>Wed</TH><TH WIDTH=50 ALIGN=CENTER>Thu</
TH><TH WIDTH=50
ALIGN=CENTER>Fri</TH><TH WIDTH=50 ALIGN=CENTER>Sat</TH></
TR>"
197:
198:ictrDay=0
199:For Row=1 to 6
200:sCalendar=sCalendar & "<TR>"
201:For Col=1 to 7
202:sCalendar=sCalendar & "<TD WIDTH=50 ALIGN=CENTER>"
203:If Row=1 AND Col=FirstDay then
204: ictrDay=1
205:End If
206:If ictrDay = 0 Then
207: sCalendar=sCalendar & " "
208:End If
209: If ictrDay <> 0 Then
210: |
If ictrDay <= DaysInMonth Then |
211: |
sCalendar=sCalendar & makAP_URL(m,ictrDay,y) |
212: |
End If |
213: |
If ictrDay > DaysInMonth Then |
214: |
sCalendar=sCalendar & " " |
215: |
End If |
216:ictrDay=ictrDay+1
217:End if
218:sCalendar=sCalendar & "</TD>"
219:Next
220:sCalendar=sCalendar & "</TR>"
221:Next
222:sCalendar=sCalendar & "</TABLE>"
223:makCalendar = sCalendar
224:End Function
225:
226:Function GMonth(iMonth, fLongShort)
227:Select Case iMonth
228:Case 1
229:If fLongShort = true then GMonth="January" else GMonth="Jan"
230:Case 2
231:If fLongShort = true then GMonth="February" else GMonth="Feb"
232:Case 3
233:If fLongShort = true then GMonth="March" else GMonth="Mar"
234:Case 4
235:If fLongShort = true then GMonth="April" else GMonth="Apr"
236:Case 5
237:If fLongShort = true then GMonth="May" else GMonth="May"
238:Case 6
239:If fLongShort = true then GMonth="June" else GMonth="Jun"
240:Case 7
241:If fLongShort = true then GMonth="July" else GMonth="Jul"
242:Case 8
243:If fLongShort = true then GMonth="August" else GMonth="Aug"
244:Case 9
245:If fLongShort = true then GMonth="September" else GMonth="Sep"
246:Case 10
247:If fLongShort = true then GMonth="October" else GMonth="Oct"
248:Case 11
249:If fLongShort = true then GMonth="November" else GMonth="Nov"
250:Case 12
251:If fLongShort = true then GMonth="December" else GMonth="Dec"
252:End Select
253:End Function
254:
255:--></SCRIPT>
256:</TABLE>
257:</CENTER>
258:</BODY>
260:</HTML>
Creating the Page
Looking at the source reveals quite a bit of VBScript code and very little HTML. Almost the entire page is generated by VBScript as it is loaded. We accomplish this feat through several routines, which are broken up into the following areas:
●Working with cookies
●Creating dynamic HTML
●Presenting information
●Tying it all together
Working with Cookies
Cookies (or more properly, magic cookies) provide a means for browsers to store persistent information without the need for access to CGI scripts, custom executables, or other means of data storage and retrieval. I use a 'variable' metaphor for working with cookies as described in the following sections:
●The Variable Metaphor
●Reading Variables
●Writing and Deleting Variables
The Variable Metaphor
An easy way to access random pieces of information from a cookie is through variable assignment. By assigning a recognizable name to persistent information, you can easily pull out relevant pieces from the cookie using a simple subroutine.
The WWW PIM uses three different types of variables to store information. It uses %%TDx where x is the record number to store to-do's, %%ADx where x is the record number for addresses, and, finally, %%APmm/dd/yyyy for appointment dates.
Reading Variables
The code in Listing 24.2 is used to read a variable from within the cookie.
Listing 24.2. The readVariable() function.
1:Function readVariable(sVariableName)
2:Dim iLocation
3:Dim iNameLength
4:Dim iValueLength
5:Dim iNextSemicolon
6:Dim sTemp
7:
8:iNameLength = Len(sVariableName)
9:iLocation = InStr(Document.Cookie, sVariableName)
10:If iLocation = 0 Then
11:readVariable = ""
12:Else
13:sTemp = Right(Document.Cookie, Len(Document.Cookie) - iLocation + 1)
14:If Mid(sTemp, iNameLength + 1, 1) <> "=" Then
15:readVariable = ""
16:Else
17:iNextSemicolon = InStr(sTemp, ";")
18:If iNextSemicolon = 0 Then iNextSemicolon = Len(sTemp) +
1
19:If iNextSemicolon = (iNameLength + 2) Then
20: readVariable = ""
21:Else
22: iValueLength = iNextSemicolon - iNameLength - 2
23: readVariable = Mid(sTemp, iNameLength + 2, iValueLength)
24:End If
25:End If
26:End if
27:end function
The readVariable() function takes a single parameter-the variable name to read-and returns the value of that variable. The readVariable() function accomplishes this through several iterations of InStr and Mid. This works fine for storing numerical information, but you run into a problem when storing random character information. Let's say you create a variable called Appt, which stores appointment information. Now if a user enters Dentist Appt in their appointment information, InStr can return the wrong chunk of information, or worse, complete garbage. The readVariable() function attempts to reduce this possibility by searching for an equal sign (=) immediately after the string being sought. To further reduce the chances of this problem occurring, routines append %% to the beginning of each variable name.
Writing and Deleting Variables
The page uses the same subroutine to both write and delete variables from the cookie. Just like those found on the store shelves, all magic cookies have expiration dates. By default, an entry in a cookie will expire when the browser is closed, which isn't very persistent. Imagine what would happen if your daily planner erased itself every time you closed the
cover.
The setVariable routine in Listing 24.3 takes a pair of parameters: The former is the variable name, and the latter its corresponding value. It then appends an expiration date of September 28, 1998, to the end of the cookie.
Listing 24.3. The setVariable routine.
1:
2:Sub setVariable(sVariableName, varVariableValue)
3:Document.Cookie = sVariableName & "=" & varVariableValue & "; expires=Monday, 28-
Sep-98 12:00:00 GMT"
4: end sub
5:
Correspondingly, the killVariable routine in Listing 24.4 sets the variable to NULL, appends an expiration date of January 1, 1995, and then calls the setVariable routine. Watchful readers will note that using killVariable will create an entry with two expiration dates. Fortunately for us, the second date is ignored and then deleted.
Listing 24.4. The killVariable routine.
1:
2:Sub killVariable(sVariableName)
3:setVariable sVariableName, "NULL;expires=Monday, 01-Jan-95 12:00:00 GMT"
4:end sub
Creating Dynamic HTML
The code uses five functions to create the statements needed to display the information in HTML format. These functions are divided into two related areas:
●Creating HREF functions
●Creating the calendar functions
The first set of functions is used to create a single <A HREF> statement that displays the stored data, if any, and provides the necessary code to edit the item.
The second set of functions is similar to the first, but also generates a table in addition to one of the HREF functions.
Creating HREFs from Scratch
The following three functions in Listings 24.5, 24.6, and 24.7 are used to create a single <A HREF> statement. The first two take only the record number as a parameter and pass back a formatted string that can be written directly into the document. Listing 24.7, makAP_URL(), takes the month, day, and year for the current record.
The code in Listing 24.6, which is used to create the address code, takes the address provided and converts it to a mailto: directive so that a user can easily mail someone on the address list by clicking on the address, or edit the record by clicking on the number.
In addition to the code to edit an appointment, the makAP_URL() function also contains an onMouseOver event to place the current appointment in the status bar.
Listing 24.5. The makTD_URL() function.
1:
2:Function makTD_URL(iRecord)
3:If Trim(readVariable("%%TD" & Trim(iRecord))) <> "NULL" Then
4:makTD_URL = "<A LANGUAGE=VBScript HREF=#null onClick=addTodo
(" &
Trim(iRecord) & ")>" & iRecord & ". " & readVariable
("%%TD" &
Trim(iRecord)) & "</A>"
5:Else
6:makTD_URL = "<A LANGUAGE=VBScript HREF=#null onClick=addTodo
(" &
Trim(iRecord) & ")>" & iRecord & ". </A>"
7:End If
8:End Function
9:
Listing 24.6. The makAD_URL() function.
1:
2:Function makAD_URL(iRecord)
3:If Trim(readVariable("%%AD" & Trim(iRecord))) <> "NULL" Then
4:makAD_URL = "<A LANGUAGE=VBScript HREF=#null onClick=addAddr
(" &
Trim(iRecord) & ")>" & iRecord & ". " & "
</a>" & "<A HREF=mailto:" &
readVariable("%%AD" & Trim(iRecord)) & ">" &
readVariable("%%AD" &
Trim(iRecord)) & "</A>"
5:Else
6:makAD_URL = "<A LANGUAGE=VBScript HREF=#null onClick=addAddr (" &Trim(iRecord)
& ")>" & iRecord & ". " & "</A>"
7:End If
8:End Function
Listing 24.7. The makAP_URL() function.
1:
2:Function makAP_URL(m,d,y)
3:Dim stAppt
4:
5:stAppt = readVariable("%%AP" & m & "/" & d & "/" & y)
6:If Len(Trim(stAppt)) > 0 AND Trim(stAppt) <> "NULL" Then