Microsoft Computing: Windows Scripting, Part II

Microsoft
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

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).

http://www.mcpressonline.com/articles/images/2002/Microsoft-VBScript2V4--09260500.png

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.








' (D)

 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

and
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

section, each component is described between and statements. You may include specifications for color, size, font, etc. for the component. You can't, however, specify where the components are to be placed within the form. You may only tell IE in what order the objects are to be laid out. Recall that IE is doing all of the object management. If the browser client area is small, the components will lay out differently than if there's more space.

Notice at statement A, VBScript lets me define ordinary VB-style variables (lName and eMail) with the Dim statement. These are global variables that may be accessed from any procedure or function.

Notice also the class ID statement contained in each of the sections (as at statement E). This is how Internet Explorer knows what kind of components to put in the form. The ID value is found in the executing PC's system registry and the appropriate component for that PC, and that version of IE is loaded into the form. The easiest way to find the component's class ID is to start an HTML document in Word and then select View/Toolbars and place a check next to the Control Toolbox entry. This will show the types of components that can be placed in the browser--labels, text boxes, combo boxes, command buttons, and so on. You can then select a component and draw it on your form. Word will generate the appropriate source code for the component and place it in an section of the page.

More VBScript Examples

Many good sources for example VBScript code are available. Two of the better resources are MSDN's scripting site and the W3School examples. Both of the common applications for VBScript are covered at the MSDN site.

If your application is more complicated or if the services of an intelligent Web server are required, you would do well to look into the more-capable ASP or ASP.NET approaches to browser-based solutions. But even so, HTML is used that is rooted on scripting basics. A familiarity with the scripting mechanisms employed will assist your understanding of the process.

Chris Peters has 26 years of experience in the IBM midrange and PC platforms. Chris is president of Evergreen Interactive Systems, a software development firm and creators of the iSeries Report Downloader. Chris is the author of The OS/400 and Microsoft Office 2000 Integration Handbook, The AS/400 TCP/IP Handbook, AS/400 Client/Server Programming with Visual Basic, and Peer Networking on the AS/400 (MC Press). He is also a nationally recognized seminar instructor. Chris can be reached at This email address is being protected from spambots. You need JavaScript enabled to view it..

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$0.00 Raised:
$