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.
JavaScript vs. VBScript
JavaScript and VBScript are the most popular scripting languages for Web browser applications. The advantages and disadvantages of each are pretty clear:
- 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.
- JavaScript is supported on almost all browsers, but if you don't know Java, learning JavaScript can be challenging.
Ironically, when you use Visual Studio (VS) to create an ASP application, VS generates JavaScript as part of the client code. That's because ASP is supported on most servers, many of which are part of non-Microsoft environments, and JavaScript will likely be supported on all of the clients.
VBScript is a purely Microsoft technology and is natively supported in Internet Explorer. It is not natively supported in other browsers. This may not be a problem for internal environments where the choice of browsers is standardized and you know IE will be used. For public applications, where the browser is an unknown, VBScript may not be acceptable. If your application is going to run on various browsers, you should probably use JavaScript because it's a more universally supported scripting language.
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
To illustrate what VBScript code looks like, consider a typical role for VBScript running in a browser: validating user input (Figure 1).
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)
*
CLASSID="CLSID:8BD21D10-EC42-11CE-9E0D-00AA006002f3">
CLASSID="CLSID:978C9E23-D4B0-11CE-Bf2D-00AA003f40D0">
*
CLASSID="CLSID:8BD21D10-EC42-11CE-9E0D-00AA006002f3">
CLASSID="CLSID:D7053240-CE69-11CD-A777-00DD01143C57">
* - Required field
CLASSID="CLSID:978C9E23-D4B0-11CE-Bf2D-00AA003f40D0">
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 between
HTML 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).
In the
LATEST COMMENTS
MC Press Online