APPENDIX A

VBScript and ActiveX Primer

This appendix provides a quick reference for developers who are starting to use the VBScript language and ActiveX technology to create dynamic Web pages.

Script Sections

Understanding scripting begins with <SCRIPT> tags, which designate sections of an HTML Web page where script code can be inserted. Although usually placed as the last set of tags in the HEAD section of a Web page, <SCRIPT> tags can be used anywhere in the page. A <SCRIPT> tag has the following syntax:

    <SCRIPT
    CLASS The style class for the <SCRIPT> tag
    EVENT The name of the event that the <SCRIPT> tag will handle
    FOR The name of the HTML element that triggers the handled event
    ID The identifier for the tag
    LANGUAGE The scripting language used for this section
    SRC The source file for this section
    TITLE Additional information
    >
    </SCRIPT>

VBScript is an event-driven language, and the primary purpose of a SCRIPT section is to map the code you write to a user interaction or a system response. Microsoft Internet Explorer supports many different types of events, including a full range of mouse and keyboard events. (For a complete list of supported events, see the online reference on the CD-ROM that accompanies this book.)

Mapping events to code in a SCRIPT section can be accomplished in different ways. You can choose, for example, to dedicate an entire SCRIPT section to a single event by using the EVENT and FOR attributes of the <SCRIPT> tag. The following code declares a SCRIPT section for the Window_OnLoad event:

<SCRIPT LANGUAGE="VBScript" FOR="Window" EVENT="OnLoad"> </SCRIPT> 

Obviously, if you declare a SCRIPT section for just one event, you will have to create multiple sections—one for each event that you want to trap. But multiple SCRIPT sections are difficult to maintain, so you might want to combine a number of events in one section. This is accomplished by using the Sub Object_EventName syntax. The following code designates a single section that contains event handlers for both the OnLoad and OnUnload events of the Window object:

<SCRIPT LANGUAGE="VBScript">     Sub Window_OnLoad()     End Sub     Sub Window_OnUnload()     End Sub </SCRIPT> 

You can also choose to map events by using event-handling attributes, which are mapped directly to a tag and call a VBScript procedure written in a SCRIPT section. The following code uses an event-handling attribute to call a function for the OnLoad event of the Window object:

<SCRIPT LANGUAGE="VBScript">     Sub PageStart         MsgBox "Page Loaded!"     End Sub </SCRIPT> <BODY LANGUAGE="VBScript" OnLoad="PageStart"> 

Regardless of how you choose to declare events, you must be wary of attempting to execute client-side code in a browser that does not support scripting. Browsers have an interesting response to tags they do not recognize: they simply ignore them. This has a strange effect on the code contained in a SCRIPT section. For example, you would expect the following code to show a message when the page is loaded:

<SCRIPT LANGUAGE="VBScript">     Sub Window_OnLoad()         MsgBox "Hello!"     End Sub </SCRIPT> 

This code works well if the browser recognizes script. But an old Mosaic browser, which does not understand the <SCRIPT> tag, ignores the tag and sees only the following text:

Sub Window_OnLoad()     MsgBox "Hello!" End Sub 

Because the code now appears as text, the old browser is happy to display the code as content in the body of the browser. This is not at all what you want. The solution to the problem is to surround the code with HTML comment marks, which hide the code from old browsers and prevent unwanted behavior. Newer browsers expect to see the comment marks and will ignore them.

<SCRIPT LANGUAGE="VBScript"> <!--     Sub Window_OnLoad()         MsgBox "Hello!"     End Sub --> </SCRIPT> 

Declaring Variables

VBScript supports only one data type: the Variant. A Variant is a variable that can represent any data—numbers, text, or objects. You simply declare a variable and assign the value. A Variant can, however, distinguish the kind of data stored in the variable. This distinction is known as the Variant subtype, which determines whether a value is added or concatenated (for example, when you use a + sign).

Data subtypes can lead to subtle Type Mismatch errors in your script code. For example, suppose you want to assign the Window object to a variable. You might declare a Variant with the Dim statement and assign the object, but this will fail unless you use the Set keyword required by VBScript when dealing with object data:

Dim MyWindow Set MyWindow = Window 

VBScript supports a full range of subtype conversions. Generally, these functions begin with capital C (for convert) and the subtype you want. Thus, a number can convert to a string, as follows:

Dim MyString Dim MyNumber MyNumber = 5 MyString = CStr(MyNumber) 

Variables declared in VBScript have lifetime and scope restrictions. Lifetime and scope refer to how long the variable is available and what parts of the VBScript code can access it. VBScript supports three levels of scope through the keywords Public, Private, and Dim.

Public variables are declared inside of a SCRIPT section but outside of any procedure. When variables are declared as Public, they are available to all the scripts in the Web page. Their values persist as long as the page is loaded.

Private variables are also declared inside of a SCRIPT section and outside of any procedure. When variables are declared as Private, they are available only to the routines defined in the SCRIPT section where the variables are defined. Their values persist as long as the Web page is loaded.

Variables declared with the Dim keyword can be declared outside of routines, where they behave as Private variables. They can also be declared directly in the body of a procedure, where they are available only to the procedure code itself; no other routine can use them. When variables are declared in a procedure, their values persist only until the procedure finishes running, and then they are destroyed.

Key Language Elements

VBScript supports a number of structures designed to perform decision and looping functions in the program. These structures create the logic of the program.

The primary decision structure in VBScript is the If…Then statement, which has the following syntax:

If condition Then     [statements] [ElseIf condition-n Then     [elseifstatements]]... [Else     [elsestatements]] End If  

If…Then structures are useful for making simple decisions but can be cumbersome if the number of cases is too large. Fortunately, VBScript supports the Select…Case statement, which provides a number of different branching tests in a single compact statement. The Select…Case statement has the following syntax:

Select Case testexpression     [Case expressionlist-n         [statements-n]]...     [Case Else expressionlist-n         [elsestatements-n]] End Select 

In addition to decision structures, VBScript supports several looping structures, which allow you to perform an operation many times or move easily through a group of objects. The primary loop structure is For…Next, which loops a fixed number of times based on the definition of a looping variable. The For…Next loop has the following syntax:

For counter = start To end [Step step]     [statements] Next 

Although For…Next is good for simple loops, more complex loops require a richer structure. The Do loop allows looping to continue until a condition is met, supporting conditional testing at either the beginning or the end of the loop. The Do loop has the following syntax:

Do [{While | Until} condition]     [statements] Loop [{While | Until} condition] 

If you need to move through a set of objects contained in a collection, choose the For Each…Next loop, which allows you to perform an operation on every member of a collection or an array. The For Each…Next loop has the following syntax:

For Each element In group     [statements] Next [element] 

Using ActiveX Components

In addition to the built-in variables, functions, and structures of VBScript, you can utilize ActiveX components in constructing your Web pages. These ActiveX components can be controls found in the toolbox of a visual development tool, or they can be Automation servers that do not have a graphical user interface. In any case, VBScript accesses the functionality of an ActiveX component through the <OBJECT> tag, which defines the component to be run on the client. The <OBJECT> tag has the following syntax:

    <OBJECT
    ACCESSKEY The key to use as an accelerator with the CTRL key
    ALIGN The alignment to use with the component
    CLASS The style class of the component
    CLASSID The Globally Unique Identifier (GUID) that identifies the component
    CODE The location of the Java class file
    CODEBASE The location of the component files for downloading
    CODETYPE The Internet media-type for the component
    DATA The Internet address where the component's run-time data is located
    DATAFLD The database field to bind to the component
    DATASRC The ID of the Advanced Data Control to bind with
    HEIGHT The height of the component
    ID The identifier for the component in this page
    LANGUAGE The language to use with event attributes
    NAME The name of the component or bookmark
    STYLE The style attributes for the component
    TABINDEX The tabbing order for the component
    TITLE Additional information
    TYPE The MIME type for the scripting engine
    WIDTH The width of the component
    > </OBJECT>

Although the <OBJECT> tag has many attributes, only three of them are required to use a component: ID, CLASSID, and CODEBASE. ID is the name of the component. You use this attribute in VBScript to address the properties, events, and methods of the component.

The CLASSID attribute contains the GUID that uniquely identifies the component. (The GUID is a serial number unique to the component and is the same on every client machine running the component.) CLASSID is stored in the system registry for all ActiveX components. When Microsoft Internet Explorer 4.0 encounters an <OBJECT> tag, it uses the CLASSID attribute to identify the component in the system registry and create a copy of it. If the component is not in the registry, IE 4.0 can download it from the address specified by the CODEBASE attribute.

Once downloaded, the component generally sets default values for its properties. This is usually accomplished through <PARAM> tags, which identify a property of the component and a default value. A <PARAM> tag has the following syntax:

    <PARAM
    DATAFLD The database field to bind to the component
    DATAFORMATAS Indicates whether the bound data is HTML or plain text
    DATASRC The ID of the Advanced Data Control to bind with
    NAME The property name
    VALUE The default value
    >


Programming Active Server Pages
Programming Active Server Pages (Microsoft Programming Series)
ISBN: 1572317000
EAN: 2147483647
Year: 1996
Pages: 84

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net