Read an Excerpt
Chapter 1: What Can Be Achieved Using VBScript?VBScript brings professional programming techniques to HTML web documents. With VBScript, we can create documents and applications that previously could only have been made available as a desktop program written with something like Visual Basic. It gives us the ability to interact with and manipulate HTML docu- ments directly from the browser. With VBScript, we can even interact with and manipulate the browser itself, sending it instructions from our VBScript program, and pulling in its variables for our own use. Let's look at a quick example of this. Figure 1-17 shows a web page that displays the date on which this particular HTML file was last modified, and also shows the referrer of this page, or the URL of the document whose hyperlink was followed to reach this page. As you are probably well aware, it is impossible to display either of these items of informa- tion with standard HTML (unless, of course, you "hardcode" them into your HTML document, in which case you're probably not really displaying the date the file was last modified or the hyperlink by which it was reached). Instead, HTML intermixed with VBScript was used to produce this page. You can see a portion of the very simple and straightforward source code responsible for producing it in Figure 1-18.
Above all, VBScript brings to us true client-side processing, so let's briefly look at some of the main uses of VBScript:
- Reference and manipulate document objects
- Reference and manipulate the browser
- Reference the contents of another loaded document or documents
- Create a document "on the fly" from the browser
- Store, reference, and manipulate data input by the user
- Store, reference, and manipulate data downloaded from the server
- Perform calculations on data
- Display messages to the user
- Access cookies easily
- Reference and manipulate a wide range of "add-on" components, both ActiveX controls and Java applets
- Display two-dimensional HTML
Reference and Manipulate Document Objects
Document objects, or, to give them their correct title, intrinsic HTML objects, are the display objects we currently use time and again in our HTML code, usually within the INPUT tag, to create forms. VBScript gives us the ability to access the properties of these objects (for example, to determine the value typed into an input text box by the user). Not only do we have access to these properties, we can also use VBScript to set the values of these properties. For example, we could set the value of an HTML text box to a particular value-perhaps to the current date and time on the client machine-as the page loads.
With normal HTML code, you can set the VALUE attribute of a text box just as easily. But unless you build your HTML source code from a script on the server (using an older technology like server-side includes, or a newer one like server-side VBScript with Active Server Pages), any value must be "hardcoded" into your HTML document. For instance, to continue with our date and time example, you could include the following line in your HTML document so that your web page displays the date and time:
INPUT TYPE=text NAME=txtTime VALUE="2/3/97 12:01:00 PM"
But unless you update your page immediately before each user accesses it, the date and time displayed when a user's browser displays the page is going to become increasingly inaccurate. Rather than using hardcoded HTML, you could instead use CGI to transmit the current date and time to display on a client machine. This is, however, the time on the server machine, and not the time on the client machine; in some cases (on slow Internet connections, in periods of heavy traffic, and, of course, in cases where the Internet server is in a different time zone than the client), the web page ends up displaying a date and time that may be more or less inaccurate. And a basic problem is that, whether you hardcode the date and time into your HTML document or whether you generate it "dynamically" through a CGI application, once the page is displayed, the value of the date and time becomes fixed.
How does VBScript help with some simple task like displaying the date and time? VBScript not only supports the standard document objects, it also allows us to attach events to these objects. To put this another way, when the user or another part of our program interacts in some way with a particular object, like a command button, the browser can automatically run a particular routine within our program. For example, if the user clicks on a certain button or moves away from a text box, an "event" is fired that causes a particular routine to execute.
The HTML code in Example 1-1 illustrates how easy it is to display the current date and time on the client machine when the document is loaded. It also allows the user to update the date and time by pressing the Update Time button.
Example 1-1: Using VBScript to display the date and time
H3 Welcome to my Web page! /H3
Reference and Manipulate the Browser
To VBScript, the browser itself is an object; in fact, it is several objects, all of which have properties. Using VBScript, you can control things such as the browser's history list and its status bar. You have tight control over how frames are handled within the browser window. You can check and reset the location or the URL, and automatically navigate to another page or even another web site. Let's say an application has one main document and two secondary documents, and that which of the two secondary documents you load depends on a combina- tion of responses from the user to the primary document (much more complex than simply clicking on a hyperlink). Automatically handling that decision process and retrieving the chosen document is an ideal job for VBScript. You can also call other VBScript programs in other frames and windows within the browser, and even open new browser windows at will, while controlling the appearance of the window at the same time. Chapter 5, Controlling the Browser, and Chapter 6, The Element Object and HTML Intrinsic Controls, provide an in-depth look at the objects exposed by the browser.
Example 1-2 contains a very simple script that controls the browser's status bar. When the user clicks the Click Me button, a VBScript routine generates a random number. If its value is less than .5, the text "Here's some text in the status bar" appears in the status bar; if its value is greater than .5, any text in the status bar is removed....