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

Schongar P.VBScript unleashed.1997

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

Name

<INPUT NAME=TxtName SIZE=60>

Address <INPUT NAME=TxtAddress SIZE=60>

City

<INPUT NAME=TxtCity SIZE=60>

State

<INPUT NAME=TxtState SIZE=60>

Zip code <INPUT NAME=TxtZip SIZE=60>

<INPUT TYPE=BUTTON VALUE="Submit" NAME="BtnSubmit"> <INPUT

TYPE=BUTTON

VALUE="Clear" NAME="BtnClear"> <INPUT TYPE=BUTTON

VALUE="Init" NAME=

"BtnInit"><BR></PRE>

Listing 18.13 shows the VBScript code that is used to process the data entered by the user. It is also used to process the mouse click events when the user clicks the Submit, Clear, or Init button. Before the days of JavaScript and VBScript, such processing would be done on the server, using CGI programs and scripts. VBScript extends the HTML code and validates user input before it is sent to the server. This reduces network traffic and improves the overall performance of your Web page.

There are two variants declared: strMsgBoxTitle and bValidOrder. Note that the types of these variables have not been defined. One can guess from the names of the variables that strMsgBoxTitle is likely to store string data and bValidOrder is expected to store a Boolean value that is true or false.

There are six procedures defined in this VBScript code: Window_OnLoad, BtnInit_OnClick,

BtnSubmit_OnClick, ValidateDeliveryDate, CheckSpecified, and BtnClear_OnClick.

The subroutine Window_OnLoad is executed when the Web page window is first loaded by the browser. It assigns the value MSFID to the variant strMsgBoxTitle. The variant StrMsgBoxTitle is assigned a string value and therefore it is now a string variable. The strMsgBoxTitle is used to display the title for the message box. Next, the subroutine Window_OnLoad calls the BtnInit_OnClick procedure. This procedure initializes the data entry fields.

The procedure BtnInit_OnClick initializes the data entry fields. It is executed when the Web page window is first loaded. It is also executed when the user clicks the Init button.

The procedure BtnSubmit_OnClick is executed when the user clicks the Submit button. It checks whether the data entered is valid. If it is valid, the variable bValidOrder that is initialized to true at the beginning of this procedure retains its initialized value. If bValidOrder is true, the order is sent. If the data entered is not valid, bValidOrder

is set to false and the control is returned to the user on the Web page.

The procedure ValidDeliveryDate checks whether the value specified in the date field is valid.

The procedure CheckSpecified is actually called by the BtnSubmit_OnClick procedure. It checks whether the data entered in name and address fields is valid.

The procedure BtnClear_OnClick is executed when the user clicks the Clear button. It clears the data entry fields.

Listing 18.13. VBScript code.

<SCRIPT LANGUAGE="VBScript">

<!--

Option Explicit

Dim strMsgBoxTitle

Dim bValidOrder

Sub Window_OnLoad

strMsgBoxTitle = "MSFTD"

Call BtnInit_OnClick

End Sub

Sub BtnInit_OnClick

TxtName.Value = "Joe Smith"

TxtAddress.Value = "1 Main Street"

TxtCity.Value = "Springfield"

TxtState.Value = "Washington"

TxtZip.Value = "12345"

TxtDate.Value = Date + 3

End Sub

Sub BtnSubmit_OnClick

bValidOrder = True

Call CheckSpecified(txtName.Value, "Please specify a name.")

Call CheckSpecified(txtAddress.Value, "Please specify an address.")

Call CheckSpecified(txtCity.Value, "Please specify a city.")

Call CheckSpecified(txtState.Value, "Please specify a state.")

Call CheckSpecified(txtZip.Value, "Please specify a zip code.")

Call CheckSpecified(txtDate.Value, "Please specify a date.")

Call ValidateDeliveryDate

If bValidOrder Then

MsgBox "Thank you for your order!", 0, strMsgBoxTitle

' TODO: Actually send the order.

End If

End Sub

Sub ValidateDeliveryDate

Dim SoonestWeCanDeliver

Dim RequestedDate

If Not bValidOrder Then Exit Sub

SoonestWeCanDeliver = Date + 2

RequestedDate = CDate(TxtDate.Value)

If RequestedDate < SoonestWeCanDeliver Then

bValidOrder = False

MsgBox "Not even we can deliver that fast!", 0, strMsgBoxTitle

End If

End Sub

Sub CheckSpecified(ByVal strFieldValue, ByVal strMsg)

If strFieldValue = "" And bValidOrder Then

MsgBox strMsg, 0, strMsgBoxTitle

bValidOrder = False

End If

End Sub

Sub BtnClear_OnClick

TxtName.Value = ""

TxtAddress.Value = ""

TxtCity.Value = ""

TxtState.Value = ""

TxtZip.Value = ""

TxtDate.Value = ""

End Sub

-->

</SCRIPT>

The remaining part of the code is again all HTML. In this example all the VBScript code is encapsulated between the <SCRIPT> and </SCRIPT> tags. The entire code resides in a single location within the body of the HTML code. This makes the code modular and easy to locate. The VBScript code is listed in the <BODY> section of the HTML code.

Now let us look at the JavaScript code for the same example. The JavaScript code is included in the file jsftd.html on the companion CD-ROM. The top part of the file is again all HTML code. The only difference is in the way the buttons are declared. You'll notice an additional criteria in the <Input> tag line-OnClick. OnClick is an event associated with the buttons. It is activated when the user clicks the button.

The second half of the file is the JavaScript code. One important difference you'll notice immediately is that everything is declared as a function, whereas we had procedures (that is, subroutines) declared in the VBScript code. JavaScript does not support procedures. It supports functions only. Every function returns a value. In this example, a return value of 0 indicates success. Overall, the form and structure of the JavaScript code are similar to the VBScript code.

Relevant Web Sites

Refer to the following Web sites for more information on Java, JavaScript, and VBScript. There are plenty of JavaScript and VBScript resources and examples available on the Net for download. Both JavaScript and VBScript are relatively new languages. You can certainly expect enhancements and bug fixes as these languages mature and gain developer support. Table 18.13 lists a few Web sites that would be excellent bookmarks for keeping up-to-date with the Java, JavaScript, and VBScript world.

Table 18.13. Web sites for Java, JavaScript, and VBScript.

Site name

Site URL

Netscape JavaScript

http://home.netscape.com/comprod/

 

products/navigator/version_2.0/script/

 

index.html

JavaScript Developer Tools

http://home.netscape.com/comprod/

 

products/navigator/version_2.0/script/

 

javascript_tools.html

JavaScript Authoring Guide

http://home.netscape.com/eng/

 

mozilla/2.0/handbook/javascript/

 

index.html

JavaScript Resources and Examples

http://home.netscape.com/comprod/

 

products/navigator/version_2.0/script/

 

script_info/index.html

JavaScript

http://home.netscape.com/eng/mozilla/

 

2.0/handbook/javascript/index.html

VBScript

http://www.microsoft.com/vbscript

VBScript links

http://www.microsoft.com/vbscript/

 

us/vbsmain/vbslinks.htm

Review

In this chapter you learned about Java and JavaScript. Although Java's portability and object-oriented capabilities are beneficial, there are approximately 3 million programmers who use Visual Basic as their primary programming language and who would benefit from using VBScript with their HTML code. Since VBScript is a subset of Visual Basic, these programmers would be able to port their Visual Basic code. Programming in Basic is a lot easier than in C/C++ for a number of the non-programmers. Clearly there is a need for VBScript-an easy-to-use scripting language that even nonprogrammers can use to build highly interactive Web pages.

In this chapter, you also learned about VBScript's programming syntax and structure. Presently VBScript is available only as part of Internet Explorer 3.0. It is available for the Windows 95, Windows NT, Windows 3.1, and Power Macintosh platforms. UNIX versions of VBScript for Sun, HP, Digital, and IBM platforms are under development. VBScript can be used with HTML code with or without ActiveX controls. It supports only one data type-a variant. You can use the function vartype to return information about the type of data stored in a variant. You can define constants in VBScript using the DIM statement. VBScript operators include arithmetic, logical, and comparison operators. The VBScript error object Err is built into it. It can be used to track runtime errors. The Err object has several properties that can be used to track and handle runtime errors. VBScript supports functions and procedures just like Visual Basic does. Functions are defined using the Function keyword. Procedures or subroutines are defined using the sub keyword. JavaScript does not support procedures.

Whether you should use Java, JavaScript, or VBScript, depends on the nature of your application and your prior

programming knowledge. If you are a proficient C++ programmer, you would find Java and JavaScript interesting and easy to catch on. On the other hand, if you are a Visual Basic guru, you would appreciate its lightweight portable subset: VBScript.

Chapter 17

Animation

by Paul Lagasse

CONTENTS

ActiveX Controls Capable of Animation

Frame Animation with Image and Timer Controls

Rotating Text with the Label Control

Using Active Movie for Digital Audio and Video

The Marquee Control for Scrolling Pages

Other HTML Tags that Provide Multimedia Playback

Now that you understand how to allocate variables and explicitly call functions in VBScript, you might be wondering how to use this new tool to effectively enhance the Web site you have or master the Web site you have in mind. The answer to that question is simple: animation. Nothing captures the attention of the transient Net surfer faster than a Web site with a lot of visual and audio data that doesn't slow down the connection.

One of the biggest drawbacks of the Internet, in its current incarnation, is the lack of effective multimedia data that can be transferred from a Web site to a local host. Without 30 frames per second animation and audio that doesn't stutter over a 28.8 modem connection, the information superhighway will never supersede the television as the world's favorite pastime and source of information. However, Internet development is happening rapidly and will soon overcome these obvious deficiencies. Tools like VBScript will be used to control the flow of this information to and from your Web site.

VBScript does not have internal structures or data types that allow it to handle digital video and audio playback, but coupled with Microsoft's ActiveX controls, many impressive types of animation can be achieved. Not only can you display an array of visual and audio data, from MPEGs to Stock Quotes, but many applications can have a level of interactivity far beyond simple frame-by-frame playback, by using Visual Basic logic. The following examples will give you some guidelines for setting up this interactivity.

ActiveX Controls Capable of Animation

We will look at a list of the current controls that Microsoft freely distributes from their Web site (http://www.

microsoft.com/activex/gallery) at the time of this writing. Using these controls together and "gluing" them

with VBScript, we can accomplish some very promising animation. As well, many updated controls are being created daily by Microsoft and third-party developers, which will make this list seem small and limited in the near future. The current animation-capable controls include

Image. This control allows the display of a variety of bitmapped image formats. In conjunction with a timer, the control can be used to create a frame-based animation or collage of images that can be manipulated by a user at runtime.

Gradient. This control displays a rectangle with a smooth gradient fill from a start color to an end color. With a

timer, this control too can be used to generate a color-cycling animation.

Marquee. The Internet Explorer Marquee control can auto-scroll (vertically or horizontally) an HTML page in a region defined by the user. The speed of the scrolling and direction are user definable and adjustable at runtime using VBScript.

Timer. The Timer is invisible at runtime and generates a specific timer event at a predefined interval. The timer event can be used to trigger activity in other controls and allows for a wide range of animation possibilities.

Stock Ticker. This control is used to display data from a URL at a given interval. This data can change frequently and will provide the user with updated information on a regular basis. The data can be in a text or XRT format on a Web site.

Label. This control displays text on an HTML page as well as responding to several types of mouse interactivity. It can be used to display the text at an angle that can be modified at runtime.

Active Movie. The Active Movie format has recently hit the scene with Internet Explorer 3.0 and allows any tools that support ActiveX to display a wide variety of digital audio and visual data. Not only does it support AVI and MPEG playback but WAV and AU audio playback as well, with little to no programming overhead.

Each one of these controls can display information statically, but in conjunction with other controls it is possible to create some fairly sophisticated animation quickly and easily. The example we will first discuss uses the Timer and Image controls to display frames of a 3-D animation rendered as Windows bitmaps. The frames of this animation can be in any bitmap format the Image control supports, which gives you the ability to display images from several sources.

Frame Animation with Image and Timer Controls

The examples in this chapter assume you are familiar with Microsoft's ActiveX Control Pad, which is an invaluable tool for laying out an HTML page that supports ActiveX controls and VBScript.

The Image control allows you to display bitmapped images in an HTML page; however, when you combine the control with a timer, it allows you to display your bitmaps as frames in an animation. The playback of these frames is completely customizable, including the order of the frames, the rate of play, and the types of images displayed.

First, the example displays an HTML layout (shown in Listing 17.1) called TimerAni that contains an Image control called Image1 and a Timer object named Timer1. These control names are the defaults generated by the ActiveX Control Pad and have not been modified to avoid any confusion.

Listing 17.1. HTML code segment to launch image control animation.

:<HTML>

:<HEAD>

:<TITLE>Image Control / Timer Sample</TITLE>

:</HEAD>

:<BODY>

:<OBJECT CLASSID="CLSID:812AE312-8B8E-11CF-93C8-00AA00C08FDF"

:ID="TimerAni_alx" STYLE="LEFT:0;TOP:0">

:<PARAM NAME="ALXPATH" REF VALUE="TimerAni.alx">

: </OBJECT>

10:</BODY>

11:</HTML>

When the layout is displayed, the Visual Basic Script will allocate two global variables to handle the processing. (See Listing 17.2.) The first, called Count, records the current frame index referenced in the second variable array, called Images(). Images() stores the names of the bitmapped graphics that contain the bulk of the animation. They are stored in consecutive order so that the frames of the animation appear to happen smoothly, but the order of the images could just as easily have been switched around, in order to make a collage of images instead of a smooth animation.

When you become familiar with this example, you can try rearranging the order.

Listing 17.2. ActiveX Layout control code for the Image/Timer animation.

1:<SCRIPT LANGUAGE="VBScript">

2:<!--

3:'Global Variables

4: dim

Count

'Keep track of current image

index

5: dim

Images(16)

'Store the names of the

bitmaps for the

animation

 

 

 

6:Images(1) = "VBSA0000.BMP"

7:Images(2) = "VBSA0001.BMP"

8:Images(3) = "VBSA0002.BMP"

9:Images(4) = "VBSA0003.BMP"

10:Images(5) = "VBSA0004.BMP"

11:Images(6) = "VBSA0005.BMP"

12:Images(7) = "VBSA0006.BMP"

13:Images(8) = "VBSA0007.BMP"

14:Images(9) = "VBSA0008.BMP"

15:Images(10) = "VBSA0009.BMP"

16:Images(11) = "VBSA0010.BMP"

17:Images(12) = "VBSA0011.BMP"

18:Images(13) = "VBSA0012.BMP"

19:Images(14) = "VBSA0013.BMP"

20:Images(15) = "VBSA0014.BMP"

21:Images(16) = "VBSA0015.BMP"

22:-->

23:</SCRIPT>

24:<SCRIPT LANGUAGE="VBScript">

25:<!--

26:Sub Timer1_Timer()

27:'When Timer1 generates the timer event, we want to change the graphic that is

currently being'displayed. To do this we set the picture path variable of

Image1 by referencing the array Images()

28: 'at the current index stored in Count

29: If Count

= 16

Then Count = 0

'Make sure the Animation loops

when it

hits

the

 

 

 

last frame

 

 

 

30: Count = Count

+

1

'Increment the

current

frame count

 

 

31:Image1.PicturePath="C:\VBSCRIPT\IMAGES\" & Images(Count) 'concatenate the current Array

32:'index the full path to the

33:'images.

34:end sub

35:-->

36:</SCRIPT>

37:<DIV ID="Layout1" STYLE="LAYOUT:FIXED;WIDTH:597pt;HEIGHT:362pt;">

38:<OBJECT ID="Image1"

39:CLASSID="CLSID:D4A97620-8E8F-11CF-93CD-00AA00C08FDF"

STYLE="TOP:17pt;LEFT:33pt;WIDTH:120pt;HEIGHT:75pt;

ZINDEX:0;">

40: <PARAM NAME="PicturePath" VALUE="C:\VBSCRIPT\IMAGES