Last month, I began an examination of coding for Windows using VBScript (see "Windows Scripting, Part I"). I used VBScript to illustrate the convenience of building a small instruction set to run in a Windows batch environment. This month, I'll move on to the more usual application of scripting: as a Web browser application. In such an application, the container for the user interface is not a standalone Windows application. The base program the user runs is a Web browser, and the "program" is a set of source code statements that are embedded within the current page's HTML. The browser interprets the instructions and creates the user interface inside the browser's display area. All the tedious details of laying out the components of the interface are handled by IE.
When to Use VBScript in a Browser
As an IT professional, you may well wonder if VBScript is something you should be interested in. Of course, the answer is "it depends." These are some of the factors to consider when evaluating a VBScript solution:
- Is the application at hand for internal use only, where all of the conditions are known? Note that VBScript is natively supported in Internet Explorer only; other browsers may not run your script.
- Is the application going to involve the participation of a Web server? If the processing is local to the client, VBScript may be a good fit. If the application includes processing provided by a server, you should probably consider an ASP or ASP.NET solution. (See MC Press Online articles on Active Server Pages and ASP.NET.) Several non-Microsoft languages are available as well.
- Is there a reason the application may not be installed on the PC? For example, a user may not have authority to install software, but Internet Explorer is already installed. If the application can be written in VBScript, the user can access it without the need for a regular installation to have taken place. Further, it can be very convenient to be able to use any nearby PC with an Internet connection, rather than one's own all the time.
- Is the application small and relatively simple? Remember, the scripting code will be embedded within an HTML page, mixed among the instructions for displaying images, the links to other pages, and the like.
- VBScript is a subset of Visual Basic (VB), so is easy to pick up if you've programmed even a little in VB. On the downside, VBScript is not supported on all browsers. Further, VBScript has been used to exploit exposures in browser security.
VBScript can be used to do most of the things that VB can do; the principle difference is that VBScript does not have its own container and process, and VB does. With some limitations, VBScript can do many of the things that VB can: call a function or procedure, compare and branch, iterate through arrays, create objects, etc. Plus, VBScript can be enhanced with Web content.
A VBScript Web Example
Figure 1: Validating user input is a common use for VBScript.
The intelligence for a modest task like that can be executed locally at the client as VBScript. For example, required fields can be checked to see that they have something in them, fields intended to accept only numbers can be checked for numeric values, and email addresses can be checked for reasonableness. The following HTML contains embedded VBScript statements to illustrate the general approach.
CLASSID="CLSID:978C9E23-D4B0-11CE-Bf2D-00AA003f40D0"> ' (E)
* - Required field
VBScript in an HTML document will take the form of either the code that describes the components of the application (text boxes, buttons, etc.) or the functions and procedures that execute in reaction to an event (user input, a mouse button click, etc.).
The code to describe components is placed betweenHTML statements (at D in the listing). The form description is part of the part of the document. To make sure that the procedures or functions that may be called are available, function and procedure definitions are usually coded into the section of the HTML document. These statements will be bounded by .
In the example listed, there is only one procedure: cmdOK_Click (statement B). This code will be executed when the user clicks the button. If the "Last name:" textbox is blank or if the email value is invalid, a message box is displayed (C).