[ Go to April 1997 Table of Contents ]|
Shop for Web Solutions
Even a novice PC user can order products, request information or search for data on the Web. But did you ever wonder what technology makes this possible? The answer is CGI (Common Gateway Interface)
The CGI standard defines new HTML tags, allowing Web pages to display a limited selection of controls. These controls include text input boxes, radio buttons and so on. In addition, a CGI protocol allows information collected by these controls to flow from the browser, through the server to a special database running on the server.
The server can validate credit card numbers, record product registration information or search the Web. The results, if any, are returned to the user's browser.
CGI has limitations. Some popular Web sites support thousands of simultaneous users. And thousands of folks feeding data to the same server program can slow browser/server dialogue to a crawl.
To compensate, developers needed a way to assign some of the server's work to the user's browser, plus a means to change Web pages without completely retransmitting the page.
Last month's column discussed part of the solution-ActiveX controls on the Web. These descendants of VBX files allow Web pages to contain a rich collection of controls previously found only in Windows programs. These controls can change their appearance by displaying new data on command, as well as provide many new ways to collect user input.
The rest of the solution consists of special Web page scripting languages. Software written in these mini-programming languages is sent to the browser, along with the rest of a Web page's information. Once received, these scripts can interact with the ActiveX controls. Best of all, the scripts execute on the user's machine, not the server, providing near instantaneous responses to user input.
VBScript is a smaller sibling of Visual Basic, used for automating Web pages. Eventually, we may see VBScript within Windows itself.
To watch VBScript in action, take a look at the program I wrote for WINDOWS Magazine's Web site. (See the sidebar "We've Got the Answers.") For a firsthand look at the site, go to http://www.winmag.com/fun/trivia1/. The program displays an array of WinMag logos on a Web page. Clicking on one of the logos reveals a question and three possible answers. If you guess right, your cumulative score increases. If not, your score decreases.
Obviously, this is not a typical Web page. It contains several ActiveX controls that display images and text. And under the hood is a VBScript program that uses the controls to detect user input and update the page's appearance. Other parts of the program determine if an answer is correct and keep score.
The program's statements reside in the same files (usually with the extension HTM or HTML) that contain the other HTML statements used to define the Web page's appearance. VBScript statements must appear between the <HTML>and </HTML>tags that mark the file's beginning and end. A file can contain several separate blocks of VBScript statements. But most programmers group all VBScript code into a single location within the file.
Unfortunately, VBScript lacks VB's slick development environment. You're pretty much on your own when you create or edit a VBScript program. Most people rely on Windows' Notepad, but any text editor will do.
All the lines between the <SCRIPT>and </SCRIPT>tags are VBScript statements. Well, almost all. The two lines reading <!&HYPH; and &HYPH;>aren't VBScript statements. In HTML, these strings are used to mark the beginning and ending of a comment block. Comments are text used to explain the meaning or purpose of a part of the HTML file. They are normally ignored by the Web browser, but browsers that support scripting languages make an exception when the comment delimiters occur between the <SCRIPT>and </SCRIPT>tags. In that case, the comment delimiters are ignored and the script statements are processed.
The delimiters are included for the sake of older browsers that don't support scripting languages and don't understand the <SCRIPT>tag. Browsers normally ignore tags they don't understand, but they will try to display any text outside those tags. Since VBScript statements fall between two tags, not inside one, older browsers will attempt to display your program rather than run it. The comment delimiters prevent that by fooling the older browser into thinking your program is just a lengthy comment.
Let's take another look at the sidebar "Quiz Show." The VBScript statements it contains define the variables used by the other VBScript statements in my quiz program. Like VB, VBScript declarations start with the venerable Dim keyword. This is followed by the variable's name.
But that's where the two languages' similarity ends. VB allows you to specify the type of data the variable will hold (String, Integer and so forth). But each VBScript variable can hold any type of data that VBScript supports (Boolean, Byte, Integer, Long, Single, Double, Date/Time, String and Object)
VBScript variables aren't, however, true Variant variables. Variant variables are the chameleons of the programming world. It's perfectly okay to treat a Variant variable as a numeric variable one minute (when performing, say, calculations), and like a string variable the next (when displaying results). Visual Basic takes care of all necessary type conversions.
VBScript isn't so helpful. All VBScript variables do have a specific type. But the type isn't determined when the variable is defined. Instead, it's determined when the variable is assigned a value. Once a VBScript variable has been assigned a value, it must be used in ways appropriate to its type. For example, a variable with the value of 7 can be used in numeric computations. But a variable with the value of "7" (a string value) cannot.
Fortunately, VBScript provides an assortment of data conversion functions. These functions, CBool, CByte, CDate and CDbl, can convert data stored in VBScript variables into most VBScript formats.
VBScript allows you to write your own procedures, too. Both subroutines and functions are permitted. The sidebar "Quiz Show II" shows a subroutine found in the quiz game. As you can see, VBScript procedures look exactly like those written for Visual Basic. The subroutine begins with the word Sub, followed by the subroutine's name (W00_MouseDown), then a list of parameters that will be passed to the subroutine (Button, Shift, X, Y). The subroutine ends with the line reading End Sub. The lines in between-in our case a single line reading Ask W00, 0, 0-are executed whenever the subroutine is called.
VBScript also allows you to write event handlers, special subroutines that respond to ActiveX controls and older CGI input controls. Event handlers are just like ordinary subroutines, except for their name. The name must start with the name of a control, followed by the name of the event to which the handler will respond. The subroutine shown in "Quiz Show II" is an event handler. As its name suggests, the handler is called automatically whenever the user presses the right mouse button (the MouseDown event) over the control named W00 (one of the question panes in the quiz)
That's all I have room for this month. Test my game on the Web and let me know what you think.
Contributing Editor Karen Kenworthy is the author of Visual Basic for Applications, Revealed! (Prima Publishing, 1994) and the manager of WINDOWS Magazine's forums on America Online and CompuServe. Contact Karen in the "Power Windows" topic of these areas, or care of the editor at the e-mail addresses here.
Sidebar -- Quiz Show
Dim Question(4,5,5) ' Qs & As
Dim Asked(5,4) ' question already asked?
Dim Waiting ' for an answer
Dim objCell ' object of current question cell
Dim RightAnswer ' correct answer to current question
Dim RightCnt ' Number of Qs answered correctly
Dim WrongCnt ' Number of Qs answered incorrectly
Quiz Show II
Sub W00_MouseDown(Button, Shift, X, Y) Ask W00, 0, 0