Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Schongar P.VBScript unleashed.1997

.pdf
Скачиваний:
45
Добавлен:
23.08.2013
Размер:
1.59 Mб
Скачать

<PARAM NAME="FontStrikeout" VALUE="0">

<PARAM NAME="TopPoints" VALUE="0">

<PARAM NAME="BotPoints" VALUE="0">

</OBJECT></CENTER>

<SCRIPT LANGUAGE="VBScript">

<!--

Sub IeTimer1_Timer()

Dim DayOfWeek

Dim MonthText

Dim strTemp

select case Weekday(Date)

case 1

DayOfWeek = "Sunday"

case 2

DayOfWeek = "Monday"

case 3

DayOfWeek = "Tuesday"

case 4

DayOfWeek = "Wednesday"

case 5

DayOfWeek = "Thursday"

case 6

DayOfWeek = "Friday"

case 7

DayOfWeek = "Saturday"

end select

Select Case Month(Date)

case 1

MonthText = "January"

case 2

MonthText = "February"

case 3

MonthText = "March"

case 4

MonthText = "April"

case 5

MonthText = "May"

case 6

MonthText = "June"

case 7

MonthText = "July"

case 8

MonthText = "August"

case 9

MonthText = "September"

case 10

MonthText = "October"

case 11

MonthText = "November"

case 12

MonthText = "December"

end select

strTemp = DayOfWeek & ", " & MonthText & " "

strTemp = strTemp & Day(Date) & ", " & Year(Date)

lblDate.Caption = strTemp

lblTime.Caption = Time

end sub

-->

</SCRIPT>

<OBJECT ID="IeTimer1" WIDTH=39 HEIGHT=39

CLASSID="CLSID:59CCB4A0-727D-11CF-AC36-00AA00A47DD2">

<PARAM NAME="_ExtentX" VALUE="1005">

<PARAM NAME="_ExtentY" VALUE="1005">

<PARAM NAME="Interval" VALUE="1000">

</OBJECT>

</BODY>

</HTML>

Creating a Random Frame Using Client-Side Refresh

Another method of creating dynamic Web pages is to use client-side refresh to periodically reload a Web page. By combining this HTML feature with VBScript code, you can produce a wonderfully dynamic page. This section describes how to use a Web page with two frames to produce a random famous quotation display. Figure 13.5 shows the resulting Web page.

Figure 13.5 : The Random Famous Quotes display page.

The main HTML file defines a frameset with two frames. Listing 13.6 contains the code for the file. The frameset refers to two frames: PAGE8.htm and PAGE7.htm.

Listing 13.6. HTML for the main page.

<HTML><HEAD><TITLE>Random Famous Quotes</TITLE></HEAD>

<BODY>

<Center><FRAMESET ROWS="15%, *" SCROLLING=NO>

<FRAME SRC="PAGE8.htm" SCROLLING=NO>

<FRAME SRC="PAGE7.htm"></FRAMESET></CENTER>

</BODY></HTML>

The top frame is static. It simply displays a heading. Listing 13.7 contains the code for this page. This file should be saved as PAGE8.htm in the same directory as the main page.

Listing 13.7. HTML for the top frame.

<HTML>

<BODY><P>

<CENTER><H2>Random Famous Quotes</H2></CENTER>

</BODY></HTML>

The bottom frame is the dynamic one. Listing 13.8 contains the code for this page. This file should be saved as PAGE7. htm in the same directory as the main page.

Listing 13.8. HTML for the dynamic frame.

<HTML><HEAD><META HTTP-EQUIV="Refresh" CONTENT="10">

</HEAD>

<BODY>

<script language="vbscript">

<!--

On Error Resume Next

DIM phrase(5)

lowerbound=0

phrase(0) = "When you have a choice and don't make it,"

phrase(0) = phrase(0) & "<br>that is, in itself, a choice.<br>"

phrase(0) = phrase(0) & "-- William James"

phrase(1) = "If you tell the truth, you don't have to remember "

phrase(1) = phrase(1) & "what you said.<br>"

phrase(1) = phrase(1) & "-- Mark Twain"

phrase(2) = "None of my inventions came by accident.<br>"

phrase(2) = phrase(2) & "They came by work.<br>-- Thomas Edison"

phrase(3) = "It is wonderful how much can be done<br>"

phrase(3) = phrase(3) & "if we are always doing.<br>-- Thomas Jefferson"

phrase(4) = "One man with courage makes a majority.<br>"

phrase(4) = phrase(4) & "-- Andrew Jackson"

upperbound=4

Randomize()

pick = Int((upperbound - lowerbound + 1) * Rnd + lowerbound)

document.write "<CENTER><TD BGCOLOR=#EEEEEE ALIGN=CENTER><font size=3>"

document.write phrase(pick)

document.write "</font></TD></CENTER>"

-->

</script>

</BODY></HTML>

The HTML tag <META HTTP-EQUIV="Refresh" CONTENT="10"> instructs the browser to reload the current document every 10 seconds. This is a browser feature and is supported by Internet Explorer.

Because the script is embedded into the <BODY> section, it executes when the page loads. The script first sets up an array of available quotations. Then the Randomize() and Rnd functions are used to generate a random number that serves as the index into the array of quotations. Finally, the document.write method is used to output the quotation and add some formatting.

Changing the Document's Colors

In addition to writing text to the HTML document, you can use VBScript to change the foreground and background colors on-the-fly. You also can change the color used to display links, but only when the page first loads. This section describes how to set the document colors within VBScript.

The example created in this section enables you to choose foreground and background colors from a drop-down listbox. You can choose from several color names in the listbox. After you make a selection, the appropriate color property (foreground or background) is set. The page created appears in Figure 13.6.

Figure 13.6 : The Color Picker Web page.

This page uses the Microsoft Forms 2.0 ComboBox ActiveX control as the drop-down listbox. Using the Microsoft ActiveX Control Pad currently is the easiest way to properly embed ActiveX controls into an HTML file. (The Control Pad is discussed in Chapter 8, "The ActiveX Control Pad.")

Listing 13.9 shows the code for the HTML file.

Listing 13.9. HTML for the Color Picker Web page.

<HTML><HEAD>

<SCRIPT LANGUAGE="VBScript">

<!--

Sub window_onLoad()

cboBGColor.AddItem "Black"

cboBGColor.AddItem "Red"

cboBGColor.AddItem "Green"

cboBGColor.AddItem "Blue"

cboBGColor.AddItem "White"

cboBGColor.ListIndex = 0

cboFGColor.AddItem "Black"

cboFGColor.AddItem "Red"

cboFGColor.AddItem "Green"

cboFGColor.AddItem "Blue"

cboFGColor.AddItem "White"

cboFGColor.ListIndex = 3

end sub

--

</SCRIPT>

<SCRIPT LANGUAGE="VBScript">

<!--

function GetColor(pstrColor)

select case pstrColor

case "Red"

GetColor = "#FF0000"

Case "Green"

GetColor = "#00FF00"

case "Black"

GetColor = "#000000"

case "Blue"

GetColor = "#0000FF"

case "White"

GetColor = "#FFFFFF"

end select

end function

-->

</SCRIPT>

<TITLE>Color Picker</TITLE></HEAD>

<BODY>

<SCRIPT LANGUAGE="VBScript">

<!--

Sub cboBGColor_Click()

window.document.bgColor = GetColor(cboBGColor.Text)

end sub

-->

</SCRIPT>

Background Color:

<OBJECT ID="cboBGColor" WIDTH=93 HEIGHT=24

CLASSID="CLSID:8BD21D30-EC42-11CE-9E0D-00AA006002f3">

<PARAM

NAME="VariousPropertyBits" VALUE="75499547">

<PARAM

NAME="DisplayStyle" VALUE="7">

<PARAM

NAME="Size" VALUE="2455;635">

<PARAM

NAME="MatchEntry" VALUE="1">

<PARAM

NAME="ShowDropButtonWhen" VALUE="2">

<PARAM

NAME="FontCharSet" VALUE="0">

<PARAM

NAME="FontPitchAndFamily" VALUE="2">

<PARAM

NAME="FontWeight" VALUE="0">

</OBJECT>

 

<br>Foreground

Color:

<SCRIPT LANGUAGE="VBScript">

<!--

Sub cboFGColor_Click()

window.document.fgColor = GetColor(cboFGColor.Text)

end sub

-->

</SCRIPT>

<OBJECT ID="cboFGColor" WIDTH=93 HEIGHT=24

CLASSID="CLSID:8BD21D30-EC42-11CE-9E0D-00AA006002f3">

<PARAM

NAME="VariousPropertyBits" VALUE="75499547">

<PARAM

NAME="DisplayStyle" VALUE="7">

<PARAM

NAME="Size" VALUE="2455;635">

<PARAM

NAME="MatchEntry" VALUE="1">

<PARAM

NAME="ShowDropButtonWhen" VALUE="2">

<PARAM

NAME="FontCharSet" VALUE="0">

<PARAM

NAME="FontPitchAndFamily" VALUE="2">

<PARAM

NAME="FontWeight" VALUE="0">

</OBJECT>

 

</BODY></HTML>

 

The script code begins execution with the window_onLoad event. This event is fired whenever the page is loaded. The code in this event adds the color names to the drop-down listboxes and sets their ListIndex properties. Doing so fires the respective Click events for the drop-down listboxes, setting the default colors to a black background with a blue foreground.

Next, a function named GetColor() is defined. This function takes a string parameter, pstrColor, and uses a Select Case to return the value for the chosen color. This function is called from the drop-down listboxes' Click event to determine the proper value to assign to the appropriate color property.

After this function, the <TITLE> tag defines the page's title and then the <BODY> portion of the HTML file starts. The first element after the <BODY> tag is another section of script code. This code is for the cboBGcolor_Click event. The code in this event sets the document object's bgColor property to the value returned by GetColor (cboBGColor.Text).

Next, some text is placed on the document to serve as a label for the background color drop-down listbox. Then the <OBJECT> element for the drop-down listbox is inserted. Note that most of the property information for this control is stored in the <PARAM NAME="VariousPropertyBits" VALUE="75499547"> element. This is obviously an encoded number. I used the ActiveX Control Pad to insert and edit this control. The assignment of this property was made by the Control Pad's Control Property feature, hiding the complexity from the Web page designer. In the Control Pad's Property window, I changed the Style property to 2 - DropDownList. This was the only property I modified from the default setting.

Next, the code for the foreground color drop-down listbox is defined. This is essentially identical to the cboBGColor_Click event except that the document object's fgColor property is used. There is then a label for the foreground color drop-down listbox and, finally, the <OBJECT> element for the drop-down listbox. This, except for the ID property (which is set to cboFGColor), is identical to the definition of the background color drop-down listbox.

After the HTML code is entered and saved, load the page into Internet Explorer. The page should load with a black background and a blue foreground. If it doesn't, first check the window_onLoad event to make sure that the proper strings are being used in the AddItem methods and the ListIndex property is being set correctly. Then check the Click event code for the drop-down listboxes and, finally, the GetColor() function. After the page loads properly, try changing the settings in the drop-down listbox. The appropriate color properties should change accordingly.

Using Cookies to Maintain User Information

Another extremely useful method for creating dynamic Web pages using VBScript is the use of cookies. A cookie is a set of information stored on the user's machine that is associated with a particular Web site. The HTTP protocol, which is the transport protocol for the World Wide Web, provides for the setting and retrieving of these cookie files. You can access this information in your VBScript code using the document.cookie property first discussed in the section "The Internet Explorer Document Object," earlier in this chapter.

NOTE

Pages that use cookies must be stored on and retrieved using a Web server. This is because cookies are tied closely to the HTTP protocol. The example presented in this chapter will not function if the file is not saved on a Web server. If you attempt to save a cookie when not on a Web server page, you receive the runtime error number 438, object doesn't support this property or method.

The cookie property is set and retrieved as a string. It can be accessed at any time using your script code. A good use for cookies is the storage of user information relative to your Web page. If you have an order-entry page, for example, you might want to store the user's name and address so that when he returns to the page he won't have to enter it again. The script code reads the cookie property, parses out the name and address variables from it, and places the resulting information in the order-entry form's input boxes. You'll see a full-blown example of doing this in Chapter 25, "Order

Entry."

Because the cookie stores multiple pieces of information within a single string, it is necessary to parse the string to retrieve the variable in which you are interested. The string is returned in the cookie property as a semicolon delimited string. Each portion of the string contains the variable name, an equal sign, and the variable's value. For example, First=Craig;Last=Eddy defines a cookie with two variables, First and Last, with values Craig and Eddy, respectively.

To parse the cookie string, I borrowed a function named ReadVariable() from Microsoft's Cookie Demo page (which you can find at http://www.microsoft.com/vbscript/us/vbssamp/cookies/extcookie.

htm). This function takes as its only parameter the name of the variable to be retrieved from the cookie string. It returns

the value of the variable or the string NOT_FOUND if the variable isn't found within the cookie string.

The simple example in this section stores the user's name and e-mail address in the cookie. It does so by providing Microsoft Forms 2.0 text boxes for the user input. After the user clicks the Save button, the information entered is stored in the cookie. Later, when the page is loaded again, the cookie information is read and if the name and e-mail variables are found, the text boxes are set to the values in the cookies.

Figure 13.7 shows the empty Web page as viewed with Internet Explorer. Listing 13.10 shows the HTML and VBScript code.

Figure 13.7 : The cookie example Web page.

Listing 13.10. The HTML for cookie example.