Using Script LanguagesPart 2: VBScript |
|
From "VS & Beyond", Access to Wang, February 1998 |
|
[ Prior Article ] [ Return to the Catalog of articles ] [ Next Article ] |
This month we will begin our look at script languages with a close look at Microsoft's VBScript. VBScript is a close cousin of Visual Basic and uses most of its syntax and design. Drawing from some of the style of the BASIC language, VBScript provides a simple language structure that is easy to learn and use.
Since VBScript is primarily known for applications that run on web browsers, the discussion and examples will focus there. As with any development that involves web browsers, the configuration of the client's system is crucial to the success of the application - the client must be properly set up for the application to run. For the purpose of this discussion, however, we will assume you are using one of Microsoft's browsers, which use VBScript without additional software or configuration effort.
Like other browser scripting languages, VBScript extends the capabilities of the primary syntax (HTML) to allow better control of field entries, screen responses, and the like. VBScript can also be used to create dynamic web pages from the server side, which makes its appeal to developers much broader.
VBScript was originally designed to work as a script and utility language for the Microsoft Internet Explorer. Like other scripting languages used in browser applications, VBScript relies on the client environment (the browser) for execution - the user's system must be ready to use VBScript or the application will not run. In addition to working with Microsoft's browsers, a plug-in is available for Netscape Navigator to allow it to use VBScript applications as well.
Since other scripting languages (most notably JavaScript) are supported by both Netscape and Microsoft products without additional effort on the user's part, VBScript has been limited in its practical application to those working exclusively with Microsoft browsers. If you use Microsoft's Internet Information Server (IIS) web server or a compatible product (including O'Reilly and Associates' WebSite Professional), VBScript can also be set up execute on the server side. When executed on the server, VBScript applications deliver ordinary HTML code to the client, allowing the developer to write interactive applications without as much concern for the capabilities of the browser.
VBScript draws from the BASIC language for some of its control structures and syntax, but uses an object model that veers sharply from the original language. In all Visual Basic languages (including VBScript), there are a series of objects that scripts are attached to and associated with. These objects have properties and functions, which define the specific element you wish to control. You direct the behavior of the object by referring to combining the object name and the parameter to be controlled into a name. For example, the document object is contains text on the user's browser, and one of its functions is write. To send a line of text to the screen, you refer to an object known as document.write. Other VBScript objects include Window and Location.
VBScript also uses methods and events, which interact with HTML and control other behaviors. Methods extend the functionality of HTML by selecting elements and controlling the cursor. Events detect user actions such as cursor movements and mouse clicks and trigger program reactions. Once these elements become familiar, other combinations of elements suggest themselves to you. With object behavior controlled by attaching scripts to the objects themselves, a sophisticated mix of events can be seen from the interaction of these objects.
VBScript uses only one data type, known as a variant. Within this one type, however, you can declare the type of information that should be present within the variable, but you cannot enforce that relationship. (Languages that require specific declaration and use of data types are known as having strong typing.) VBScript does provide strong functions for data conversion, so all incoming data should be converted to the proper type before storage, comparison, or other use.
Like other versions of Visual Basic languages, data elements can be defined before use or created as needed. A script option ("option explicit") creates error messages for undefined elements.
Like many other languages, VBScript does not provide a data type for constants (data items that should never be modified). Instead, Microsoft recommends consistent variable naming practices so that the types and uses of variables are known from the name.
VBScript provides a number of ways of controlling program flow. The most powerful of these is the If...Then...Else structure, which can be used for simple decisions or more complex, case-like structures. Other program control structures include Do...Loop, While...Wend, and For...Next. (The specifics of these constructs follow other languages and will not be covered in detail here.)
An important feature of the If...Then...Else structure is the ability end the If clause specifically - a function notably missing from the original BASIC syntax and COBOL prior to the 85 standard.
Besides the screen object (document), VBScript can also issue alerts - dialog boxes that appear above the browser window. With the aid of other ActiveX components (Microsoft's fundamental technology for application support), VBScript can also provide more sophisticated control of screen elements (including exact placement of text and graphics). VBScript can also interact with JavaScript scripts that control screen activity.
VBScript can extract a number of run-time elements such as the date and time, browser type, and other information typically carried in the web page header. In some circumstances, VBScript can also write information to the client system; this information can be in form of cookies (application-specific data used by browsers) or even text and other data.
VBScript applications can be developed with nothing more than a text editor and a copy of a compliant browser. Microsoft provides some script debugging capabilities within the interpreter built into the browser, but it's more of an annoyance than a benefit. There is also a script debugging tool available (see the reference to the Microsoft Scripting web page, below), but it does not provide a truly integrated debugging environment.
Microsoft's Visual InterDev - a cross-application development tool aimed at professional web developers - provides some support for VBScript and other syntaxes by displaying a color-coded view of the source. It will also display previews, run the browser session, and capture and display results.
The sample script below (Figure 1) shows a typical situation in web development - the need to check a form for proper entry. If the name field is left out, a dialog box appears (using the msgbox function) to inform the user. Other processing logic (including the final action to be taken) has been left off for clarity.
Formerly a tool for Intranet developers (or others that could specify browser type), VBScript has become a generalized scripting language for server-side scripting.
Microsoft VBScript Home Page
http://www.microsoft.com/vbscript/
Primary location for information specific to VBScript, including a knowledge base and sample code.Microsoft Scripting Home Page
http://www.microsoft.com/scripting/
Covers all of the Microsoft scripting languages, including JScript (their version of JavaScript) and VBScript. Many free code samples and a script debugging application.Microsoft Internet Information Server
http://www.microsoft.com/iis/
Information and free downloads for the Microsoft server and its related BackOffice components and products.O'Reilly & Associates WebSite
http://website.ora.com/
A web server for Windows NT with support for a number of scripting languages (including VBScript) and Active Server Pages.
Figure 1: Sample VBScript Application
<html> <head> <title>Sample Form Using VBScript</title> </head> <SCRIPT LANGUAGE="VBS"> sub submitbutton_OnClick If document.myForm.username.value = "" Then msgbox "You forgot to enter your name", 0, "Application Alert" End If end sub </SCRIPT> <body> <h2>Sample Form Using VBScript</h2> <p>Please fill in the items below. The name field is required for processing.</p> <form name="myForm" action="sample.htm"> <table border="0"> <tr> <td><strong>Name (required)</strong></td> <td><input type="text" name="username" size="20"></td> </tr> <tr> <td><strong>Address</strong></td> <td><input type="text" name="useraddress" size="20"></td> </tr> <tr> <td><strong>City, State, ZIP</strong></td> <td><input type="text" name="usercity" size="20"></td> </tr> <tr> <td colspan="2"> <input type="button" value="Submit Entry" name="submitbutton"> <input type="reset" value="Reset" name="reset"> </td> </tr> </table> </form> </body>