by Warren Ernst
Somewhere between the full-blown intelligence programmed into ActiveX controls, and the static text-display capabilities of HTML, lies the useful, simple, and flexible world of Web page scripting. With
Web page scripting, you can "program" Web pages to perform actions without compiling dozens (or hundreds) of lines of complicated codeinstead, the script transfers within the text of the HTML file, then "runs" as the page is
displayed. The beauty of scripting in Web pages is that it's relatively fast and easy, requiring no special modifications to the Web page server; you need to add only enough lines of scripting code to get a job done, without a lot of extra overhead. You
can have a Web page script perform useful tasks with as little as three lines of code. Web page scripts can also act as a sort of "glue" for adding larger, preprogrammed objects, such as Java applets or ActiveX controls, into your Web pages,
without having to create custom controls or applets yourself.
The suite of ActiveX methods includes a new scripting language for the Internet: Visual Basic Script (or VBScript, for short). Although the more established scripting language JavaScript can be used
to glue ActiveX controls together, VBScript brings to the table many advantages over JavaScript, including increased speed, a more shallow learning curve, and a tight coupling to the rest of the ActiveX environment through the use of OLE. In fact, with OLE you can even use VBScript to work with desktop applications that aren't conventionally used over the Internet.
Much of the new functionality you're starting to see in Web pages is actually Web page scripting, rather than full-blown Java applets or ActiveX controls, since it's easier to include scripted code
in a Web page than write an entire applet or control. For example, to perform a simple calculation, someone familiar with writing scripts can create a useful script in a couple of minutes instead of the many hours (or more) it would take for an applet.
Unfortunately, JavaScript, the Web page scripting language built from the Java language, might not be the most suitable language for scripting because of its object-oriented heritage. This section covers why Web page scripting is
useful, outlines general scripting uses, and explains why JavaScript might not be the best scripting solution in many cases.
There are many properties inherent in all Web page scripts (regardless of the language they are written in) that make them an exceptionally useful tool in the Web page writer's toolbox. First and
foremost, scripts enable client-side computing within Web browsers; in other words, scripts make it possible for Web browsers to do some computing themselves instead of relying on the Web page server.
Either by performing computations and generating output themselves, or by initializing and running applets or controls, scripts normally start off the computing process within the Web page.
Using scripts in combination with controls or applets is especially useful because it allows you to write fairly generic controls that can be reused, as opposed to writing and compiling a new and
unique control for each Web page that performs a similar function. For example, you could write a control or applet that displays scrolling text in a box, but not hardcode the message, font, or size (for example) directly into it. Instead, you could write
the control to expect these (and other) generic parameters from the Web pages they're used in, and these parameters can be delivered to the control by a script, as shown in Listing 7.1.
<applet codebase="./LEDSign/LED" code="LED.class" width=500 height=48 align=center> <param name="script" value="rumor.led"> <param name="border" value="2"> <param name="bordercolor" value="0,200,0"> <param name="spacewidth" value="3"> <param name="wth" value="122"> <param name="font" value="./LEDSign/fonts/default.font"> <param name="ledsize" value="3">
Scripts are also simpler to implement in a Web page than writing a full-blown control or applet, so using them to create some smarts in a Web page can take far less time for a developer. The reasons
for this are twofold: You typically need just several lines of scripting code to perform an action, and there's no need to generate bytecode or executables with a separate compiling program. Scripts are interpreted by the Web browser to perform a
computation or action, much the way HTML tags are interpreted by the browser to display text in the content area. Additionally, since scripts aren't compiled, there aren't any extra overhead library functions that might add unnecessary size to the
programming module; this keeps the file transmission simple and fast.
Web page scripts are normally written into the HTML file itself, which means that your program, when written as a script, will never be separated from the Web page you
designed it for, regardless of the quality or speed of the Web browser's network connection. Only one file is being transferred to create a page, not several different ones. Occasionally, applets and controls get sent incompletely through slow or unstable
Internet connections to viewers. As a result, the page's functionality gets lost and won't workwhich is something that will never happen with a script. The drawback to linking script code to Web pages is that you must include multiple copies of a
script when you want to use it again on another page. This requirement means maintaining multiple copies of the same script, which can be troublesome.
So far, this discussion has centered on the capabilities of Web page scripts in generalcapabilities that should be available to programmers regardless of the actual
scripting language selected. Unfortunately, some of these "natural" scripting properties are less evident when using the popular Web page scripting language JavaScript, mostly because of its Java-based heritage. Certainly, you can use JavaScript
in combination with ActiveX controls and other VBScript-based ActiveX scripts in the same page, so if you're already well-versed in JavaScript, you can use it to initialize ActiveX controls, create engines for Web page forms, or generate special effects.
On the other hand, if you aren't already skilled in JavaScript, learning it so you can make use of applets or controls is not without pitfalls. For starters, detailed JavaScript scripts require some
sort of knowledge of C++ and object-oriented programming skills because JavaScript's syntax and organization carry over directly from Java, which is mostly derived from C++. Listing 7.2, a JavaScript script that scrolls text along the bottom of the Web
browser window, is 36 lines and reads very much like C++ code.
/* Copyright 1996 Q&D Software Development All Rights Reserved. */ function scroll_status (seed) { var msg = "This is the scrolling message text"; var out = " "; var c = 1; if (150 < seed) { seed--; var cmd="scroll_status(" + seed + ")"; timerTwo=window.setTimeout(cmd,100); } else if (seed <= 150 && 0 < seed) { for (c=0 ; c < seed ; c++) { out+=" "; } out+=msg; seed--; var cmd="scroll_status(" + seed + ")"; window.status=out; timerTwo=window.setTimeout(cmd,100); } else if (seed <= 0) { if (-seed < msg.length) { out+=msg.substring(-seed,msg.length); seed--; var cmd="scroll_status(" + seed + ")"; window.status=out; timerTwo=window.setTimeout(cmd,100); } else { window.status=" "; timerTwo=window.setTimeout("scroll_status(150)",100); } } } // -- End of JavaScript code -------------- -->
The flip side of JavaScript's similarity to C++ is that object-oriented languages tend to be very structured, which is a good thing. As languages, Java and JavaScript are very structured and give you clean approaches to common problems associated with
other procedural languages, like Pascal and Basic. However, structurued object-oriented programming makes more sense in a full-blown development environment, such as a Java applet or application. For quick solutions in the context of a Web page, the object-oriented nature of JavaScript is of little added benefit, unless you're already an object-oriented programming whiz.
ActiveX is intended to make it easier to create real computing power within Web pages, and the suggested scripting language of ActiveX, Visual Basic Script (or VBScript), follows this same
philosophy. VBScript combines the simplicity of Visual Basic with all the power of JavaScript and then some. Therefore, you can continue using JavaScript in ActiveX-equipped pages, but take advantage of all the benefits VBScript has to offer.
Perhaps the best thing about using VBScript is that it's based on Visual Basic, a popular and simple programming language with a large
following. VBScript is actually a subset of Visual Basic for Applications (VBA), the scripting language built into most Microsoft business applications, which is itself a subset of Visual Basic 4 for Windows. A complete list of functions and features that
are and aren't in VBScript, VBA, and Visual Basic 4 can be found at http://www.microsoft.com/INTDEV/vbs/vbscript.htm#lang.
Therefore, because of its heritage, if you already have some experience with either creating applications with Visual Basic or creating application scripts, you will find VBScript easy to use immediately. On the other hand, if you don't have any Visual
Basic experience, you'll find that learning VBScript is a great first step to getting acquainted with the full Visual Basic 4 programming language to easily create full-blown applications or controls later.
Of course, as with any scripting language, you will need some sort of programming experience to use VBScript effectively, but VBScript is simple enough that it shouldn't hinder the learning process. VBScript uses real English, for the most part, and
never requires you to learn object-oriented methods or obscure notations like >> or %=, as you would with JavaScript. Again, this may be a benefit to non-programmers or an annoyance to anyone with experience in programming languages beyond Visual
Basic. The assumption is that hard-core programmers will opt to write ActiveX controls with a more structured object-oriented language like C++, but non-programmers will stick with VBScript. Incidentally, this situation directly parallels the relationship
between the users of Java and JavaScript.
VBScript scripts work similarly to scripts created with other Web page scripting languages. Scripts are written in plain text and embedded
within Web page HTML files; they are interpreted and "run" by the VBScript language engine built into the Web browser as the HTML file is transferred.
VBScripts are embedded within an HTML file by using the <SCRIPT> HTML tag, which includes the following parameter: <Language=VBS>. This parameter tells the browser that everything between the <SCRIPT> and </SCRIPT> tags in the HTML file is handled by the VBScript language engine, which then interprets the script and
generates results immediately.
For example, here's a small (but complete) HTML file with a complete, though small, VBScript script written between the <SCRIPT> tags. It executes upon clicking the On-Page button, which is generated with conventional <FORMS> commands.
<HTML> <HEAD> <TITLE>A Web Page with VBScript Code</TITLE> <SCRIPT LANGUAGE="VBS"> <!-- Sub BtnHello_OnClick MsgBox "Hello, world!" End Sub --> </SCRIPT> </HEAD> <BODY> Here is some body text<p> <FORM><INPUT NAME="BtnHello" TYPE="BUTTON" VALUE="Click Here"></FORM> </Body> </HTML>
When this page is transmitted to a browser with a VBScript language engine, the script written between the <SCRIPT> tags is interpreted by the VBS language engine and tied to the click event
on a button named BtnHello. Therefore, the message Hello, world! appears in a text box when the button is clicked. (See Figure 7.1.)
Figure 7.1. The HTML file from Listing 7.3, in a VBScript-enabled browser once the button has been clicked.
Larger, more detailed scripts work the same way, with the scripting occurring either in the Head or Body sections of the HTML file, depending on when the script is
supposed to "run."
Visual Basic scripts can do far more than the simple example above, and can even do more then JavaScript scripts.
First, VBScript can validate Web page form data, performing calculations, checking lookup tables, or simply verifying the existence of information in a particular form field. Since it can do all this without intervention from the Web page server, it
can reduce the server's workload, making the rest of your pages transmit faster. This is an important capability of VBScript because anything that can be easily relegated to the client end of a Web connection will greatly improve performance. Not only does
it help minimize server workload, but it also decreases the amount of information being sent back and forth to the server.
VBScript can also interact directly with the browser and make it perform actions. For example, it can look at how a form is filled out, then automatically fill out other parts of the same form
on the same page, depending on what is selected. (See Figure 7.2.) In this case, selecting an option in the left column automatically selects or deselects items in the right column, generates a name for the combination in the bottom field, and calculates a
cost based on the selected items in the right column. It all occurs interactively with VBScripts.
Figure 7.2. In this demonstration Web page, VBScripts calculate amounts and select checkboxes as soon as they detect a change in the form, without page-reloading delays.
Finally, VBScripts have the muscle of OLE behind them, so they can communicate with your OLE-enabled desktop applications through a soon-to-be-available OLE Scripting Manager. This tool will
manage the technical details of getting and calling OLE entry points and managing the namespace available.
But the true power of all these features is that they are accessible to programmers with a language much easier to learn and use than JavaScript: Visual Basic Script.
This chapter has discussed the benefits of using VBScript to script simple computing tasks and to act as a "glue" for ActiveX controls. The next chapter, "ActiveX Internet Information Servers," explains what an ActiveX-enabled Web
server is and discusses the Internet Information Server and ISAPI filters.