Chapter 21

VBScript and the Internet

by Keith Brophy and Timothy Koets


CONTENTS

The Microsoft Visual Basic Scripting Edition, more commonly referred to as VBScript, is one of several revolutionary new scripting languages at your fingertips today for producing exciting, dynamic Web pages. If you've heard a lot about VBScript and what it can do, but you've been bombarded with so much new information that you can't absorb it all, you're not alone. This chapter was written for you! It will clearly and methodically explain to you the essence of VBScript-that is, what it's all about. Here you will see, in plain English, why VBScript is exciting and what it can do for you. Some of the questions answered in this chapter include

This chapter presents an overview of VBScript, followed by a condensed guide to the entire VBScript language in Chapter 22, "The VBScript Language." Then, in Chapter 23, "VBScript Application Pages," you will see VBScript in action and examine several Internet applications built using VBScript.

What Is VBScript?

When the World Wide Web first became popular, HTML was the only language programmers could use to create Web pages. They soon learned that HTML had some major limitations. Although HTML presented the user with a "page" of information, the Web page and the user had a limited amount of interaction; the experience was like reading the front page of a newspaper on a computer monitor. Now most computer users, whether they use Windows, Macintosh, UNIX, or a combination of the three, are accustomed to graphical applications that provide interaction. They're used to clicking buttons, entering values into text boxes, and choosing from menus. In fact, the only way to get useful work done with a computer is to interact with it. The first generation of Web pages provided information to the users, but the users could not interact with the Web the way they could with their word processors. If users wanted to interact with their Web pages, they had to send the data to the server, which contained all the "smarts." The server sent its results back to the Web page. This interaction required a great deal of extra time, effort, and overhead, and the user interface was very constrained compared to other popular applications.

Fortunately, the builders of the Internet and the World Wide Web recognized these limitations. They soon realized that if the user was denied the capability to interact with the Web page, the Web itself would become little more than a collection of information, much like a library of books. Although that collection is very useful, users demand more from their computers than a duplication of what they could find elsewhere.

The capabilities of HTML began to grow and become more powerful. Soon, designers began to realize that they needed more than just HTML to make the Internet accessible and useful to the masses. Corporations that wanted to develop enterprise solutions or explore the profit potential of the Internet also began to pressure designers to give them something more.

These demands have resulted in a continued improvement of HTML, the emergence of browsers such as Internet Explorer that tap into the power of HTML, and the advent of scripting languages such as VBScript. To understand what a scripting language is, think of HTML as an airport runway. You can get where you need to go on the ground, but you have an entire sky to travel through above you. Scripting languages are like the airplanes that enable you to lift off the ground. They extend the capabilities of the Web much like an airplane enables you to travel through the sky. A script lets the page become an active, dynamic piece of software, rather than a static piece of content.

NOTE
A scripting language is a special type of programming language used to provide control in another host environment. It is interpreted rather than compiled. Therefore, a program built with a scripting language must be run in the environment that contains the scripting language's interpreter and cannot be run as a stand-alone application

HTML can't interpret a scripting language itself, but it knows enough to call the interpreter of the scripting language to carry out the interpretation. Consequently, you can overcome HTML's limitations by using any browser-supported scripting language to extend the Web page. With scripting languages such as VBScript, the limitations of HTML disappear; the opportunities are now limited only by the power of the scripting language! Although other scripting languages are bound to emerge, two widely used scripting languages in existence today are JavaScript and VBScript. Both of these languages can be embedded in a Web page, and if the browser supports them, they provide the path to smart, active programs that are part of that page.

With HTML you can place controls such as buttons and text boxes on a Web page. Without a scripting language such as VBScript, any actions the user takes on the controls of a Web page must be sent back to the Web page server. That is, the user's computer cannot handle them. Furthermore, the amount of control and flexibility available without a scripting language is very limited. With VBScript you can link to controls on the Web page and also write code to respond to what the user does with those controls. If, for example, a Web page contains a command button, you can write VBScript code that gets executed immediately when the user clicks that button. An example of such a code segment might look like the following:


Sub Button_OnClick

   ' The message below will be displayed when the user clicks on the button

   Msgbox "This button was clicked"

End Sub

Button is the name of the button, and OnClick is the event that the user causes to occur when he or she clicks the button. You can supply code such as the message statement shown here to be executed every time the user clicks the button. The Msgbox statement, which is discussed in detail in Chapter 22, simply presents a message box to the user. This example is simple, but it shows how VBScript code can enable a page to respond immediately to user actions. VBScript breathes life into otherwise static Web pages, making them dynamic, responsive, intelligent, and interactive.

What Can VBScript Do?

VBScript lets the user interact with a Web page rather than simply view it. Many scenarios are possible for this interaction. For instance, Web pages can ask questions and respond to the user's answers. A VBScript can then take input from the user and check the data to make sure it is valid or meets some criteria. Then it can put an Internet server to work by actually storing the data or causing some action to take place on the server based on the user's input. For example, VBScript could respond to a user's request for an airline reservation by reading in the data, checking to make sure that the request is complete and that the phone number and ZIP code are in a valid format, informing the user of the estimated price, and then notifying the server of the reservation. All these tasks could be performed by the code in the Web page that was downloaded across the Internet as it sits on the user's client PC. The server, in turn, makes sure that the ticket is available, books the flight, and arranges to deliver the tickets to the customer.

Interaction can also be helpful for advertising services or products to a user. A business can use an interactive survey to deliver carefully targeted messages to potential customers. Imagine, for example, a series of Web pages that ask you qualifying questions about your dream car, along with how much you want to spend, and then display information on vehicles that match your criterion. Rather than dealing with a pushy salesperson, you can take your time on the Internet and carefully obtain the facts you need. When you finally go to the dealer's showroom, you can use your knowledge to get the best price.

In this type of interaction, VBScript can play an important role in many ways, including validating data, costing, providing impressive multimedia feedback, and initiating data storage. You can use VBScript to sequence the questions based on responses. For example, if a user wants a van, VBScript can generate an input box that asks him or her to specify the number of seats needed. Throughout the data entry process, VBScript can make sure the user enters a valid order, address, and method of payment, and it can even display a pie chart of how much of the cost goes toward the base price and how much goes toward extras. The sound of a trumpet fanfare can announce the bar graph. The possibilities are endless.

VBScript can also perform calculations on data, such as computing the cost of an item that includes the sales tax. Often, calculations on a Web page enable users to figure out what they want to do or perhaps give them some sort of result. A Web page enables the user to walk away with more than a mere presentation of fixed information. You could, for example, design a Web page that enables customers to choose luxury items for a car and, as the luxury items are selected, adjusts the overall cost. The users could spend as much time as they want and choose as many combinations as they like until they find the perfect combination of features versus price. How often can someone get that level of service from a car dealer?

By utilizing other technologies such as CGI, or Common Gateway Interface, which transmits information from the client back to the server, VBScript code can even initiate order placement for that item in the vendor's computer. If the script determines the order meets all criteria for a valid order, it can place the order. Otherwise, it can generate an error message. Using script logic, it could even place the order on a different server, depending on which type of car was requested, and provide an estimated time frame for delivery based on a rule-of-thumb calculation for that type of order. Visual Basic can perform virtually any process that a traditional application can perform. Even in areas in which Visual Basic can't directly cause some action, such as writing to the server database or playing a sound file, it can achieve these results indirectly by using CGI scripts or sound controls, for example. Visual Basic becomes the application behind the Web page that interacts with the user.

Another important aspect of this programming model is that you can also use intrinsic HTML form controls and Microsoft's ActiveX controls with VBScript to give Web pages an attractive look and feel. Intrinsic HTML form controls give the Web page developer a standard set of controls similar to those used in the Windows environment. ActiveX controls include graphs and charts, labels that can be rotated 360 degrees, "new" banners that can remain on a Web page for however many days you want, a timer that enables you to time events on Web pages, a pre-load function that lets you load bitmaps and other time-consuming parts of a Web page before it gets displayed, and so on. These controls give Web pages a professional, polished look. They also provide pages with smarter interactive responses because a Visual Basic program can maintain dynamic control over the control characteristics. For example, your script code can generate a new graph based on user input.

You can also use Microsoft's vast array of OLE controls in VBScript, which opens up a whole world of possibilities for Web pages. Designers can now place ActiveX custom controls directly on Web pages in the Windows environment. OCXs, the forerunner to ActiveX controls, have made languages such as Visual Basic incredibly powerful because programmers can "glue" an OCX that performs some task for them, such as displaying a calendar on the screen, into the application. They don't have to create code to put a calendar on the screen; the OCX does the job for them. Likewise, programmers can now put an ActiveX control on a Web page and access the control through VBScript. The end result is better, more interactive pages for the end user.

In addition to using ActiveX controls, VBScript can also tie other applications into a Web page through OLE automation technology. For example, with the appropriate object declarations, you can tie an Excel spreadsheet into your Web page so that when the user clicks on the spreadsheet, Microsoft Excel runs and loads the spreadsheet for you to edit. Now, you can work within a Web page and also activate other applications within the Web page with the click of a mouse button. The ability to tie external applications to a Web page enables you to show virtually anything on a Web page; the only requirement is that the applications you want to link to a page support the OLE automation standard.

With VBScript and the right controls, you can even create 3-D animation effects, making your Web page come alive with moving objects in response to certain events. You can use animation to make cars careen across the screen, butterflies fly across your Web page, or arrows move and point to where you want the user to interact with the page.

The component incorporation capabilities of VBScript introduce some special considerations and trade-offs in page design. For example, a Web page that includes VBScript code that interacts with Excel and Microsoft Word and uses ActiveX controls (which are currently only supported within Windows), cannot be fully distributed over the Internet with all the support software. The Web page might not run properly on all of the various platforms and operating systems. Still, a page that leverages these technologies would be fully usable for Internet or intranet users who have the necessary hardware and software to successfully support the Web page in question.

At the other end of the spectrum, a page that incorporates VBScript to carry out a series of calculations can be a perfect Internet citizen, fully downloadable over the Internet and running on a variety of platforms and operating systems. In this respect, the flavor and tone of your VBScript Web pages depend largely on how you want to leverage them.

NOTE
Microsoft might support ActiveX controls on other environments such as the PowerMac in the future, but they are currently supported only in the Windows environment

In order to gain a more complete understanding of how VBScript works with browsers, controls, and objects, consider the simple model shown in Figure 21.1.

Figure 21.1 : A simple diagram of the VBScript<\->host model.

The innermost box contains the core language of the scripting language which, in this model, is VBScript. Other languages, such as JavaScript, could be placed in this box as well. This core language cannot be run alone-it requires the support of a host, which leads to the second, larger box in the figure.

The second, larger box represents the host program that runs the scripting language. At the time of this printing, VBScript could be used with Microsoft Internet Explorer 3.0, which in this case would be the host. Although the host might vary, the core VBScript language stays the same. In other words, the VBScript language is the same regardless of the host. The host might contain a set of internal objects and controls. For example, you can access Internet Explorer's document object from VBScript, which is discussed further in Chapter 22. You can also access Internet Explorer's Intrinsic HTML Controls through VBScript, also covered in the next chapter. Any scripting language, such as JavaScript, has the same type and level of access to these browser objects and controls as VBScript has.

The third box represents components external to the browser but available in the Windows environment, such as ActiveX controls, OLE controls, and objects such as OLE automation servers. Figure 21.1 illustrates how VBScript can access these controls and objects through the browser. In order for VBScript to access a control at this level, it must be linked through the host. Each link of the chain must be complete before VBScript can access any objects or controls throughout the chain. Because the browser provides a secure environment, the user will be warned before a script attempts to use a control, provided the default browser options are in place to do so.

Remember that any scripting language operates using this model. JavaScript, another powerful scripting language, would occupy the same position in the figure as VBScript does. Consequently, any scripting language has access to the browser's controls and objects as well as to the system's controls and objects if they are supported by the browser.

The variety of controls and technologies that surround the World Wide Web is likely to increase dramatically over the next several years. Although keeping up with the changes and new controls on the market can be quite dizzying, it's an exciting time to be a part of the Internet revolution. Now that you have had a glimpse at the power of VBScript and the technologies that surround it, the examples in the next section will help you understand further some of VBScript's capabilities!

Learning VBScript

VBScript is much easier to learn than programming languages such as Java, C/C++, and other scripting languages such as JavaScript. Derived from the BASIC language, VBScript should not be difficult for anyone who has any computer programming experience.

To start working with VBScript, you need several things: a browser that supports VBScript, the VBScript runtime interpreter, access to required controls, and an editor or other tool to help you assemble Web pages or edit HTML documents. The first step is to obtain a browser that includes VBScript runtime support if you do not already have one. Microsoft's Internet Explorer 3.0 was the first publicly available browser that supported VBScript. This browser is available free from Microsoft. At the time this was written, you could obtain Internet Explorer 3.0 from the Microsoft Web site at http://www.microsoft.com/ie.

Any browser that supports VBScript includes the VBScript runtime interpreter. Therefore, if you install the browser, you have everything you need to run VBScript, including the runtime interpreter. You don't need to obtain any other pieces. However, you might want to refer to the location http://www.microsoft.com/vbscript for a general description of runtime capabilities beyond that provided in this book. The runtime interpreter for VBScript is license free, just like Internet Explorer. Even if other browsers incorporate the VBScript runtime interpreter, Microsoft does not charge a licensing fee to the end user or the company that produces the browser. Therefore, you can safely bet that the VBScript interpreter will be widely distributed with most browsers in the future.

If you've obtained Internet Explorer 3.0, you're nearly all set to run VBScript programs. You also will need ActiveX controls from Microsoft for some of the samples in this book. You can set up pages with control definitions to automatically download required controls from across the Internet. The requirements page included with the VBScript samples on the CD-ROM uses this technique.

If you are a VBScript programmer-in other words, you want to edit VBScript programs or create your own from scratch-then you need one more tool, a Web page editing tool. Such a tool not only enables you to generate Web pages but also enables you to embed VBScript code into those pages. Fortunately, most tools provide this capability. As long as you can enter text insertion mode and type text directly into your page as you create it, you can insert VBScript statements. You can use many different tools to accomplish this task. They range from the Windows Notepad, a simple text editor, to more sophisticated tools such as Internet Assistant, a utility that incorporates itself into Microsoft Word and enables you to quickly and easily build Web pages.

Another available tool for use in Web page design with VBScript is Microsoft's ActiveX Control Pad. This tool is an HTML text editor that enables you to insert HTML and ActiveX controls automatically into your HTML documents with the help of a wizard. Figure 21.2 shows the beta version of the ActiveX Control Pad in action.

Figure 21.2 : Microsoft's ActiveX Control Pad in beta form.

The editor has many additional features that aid in editing Web pages and incorporating VBScript and JavaScript into them. Another useful feature Microsoft has provided in the ActiveX Control Pad is the capability to place controls on a Web page using the Microsoft Layout Control. This placement capability in the Control Pad, together with Internet Explorer's 2-D layout for HTML, enables you to position elements and controls anywhere on a page in a form-like fashion, much like Visual Basic. The layout information is built in a separate file that the layout control interprets and incorporates into your main page when that page is loaded. This approach is discussed in more detail in the next chapter.

Security and VBScript

One of the many questions people ask about VBScript is, "How secure is it?" VBScript was designed as a subset of the Visual Basic language. When you look at the language and compare it to VBScript, you essentially see a stripped-down version of Visual Basic. The designers took any part of Visual Basic that could cause VBScript to be unsafe and insecure and eliminated it. The end result of their work is a language that is safe and "lighter" than its parent, Visual Basic.

When people think about safety and the Internet, their concerns are often valid. The Internet has just recently become popular to the masses, and companies are starting to think of ways to make money on the Internet. This brings to mind images of users entering credit card numbers, making banking and shopping transactions, and paying for other services. Obviously, before such activities can take place, the Internet must be secure. Otherwise, people simply won't want to take the chance of their credit card numbers being intercepted or some secure password being used to access their accounts. The World Wide Web and Internet consortia are working very hard to establish security mechanisms for the Internet. Rest assured; security will be an area of intense interest in the short term until the issues can be firmly resolved.

Users of VBScript are also likely to be concerned about security. They want to make sure that VBScript does not open the door for a devious Web page to damage their computer systems in any way. The most common type of computer damage users fear is the multitude of viruses that are transmitted to computers when files are downloaded from an Internet server and modified on the user's computer. Other possible damage includes a Web page that, for some reason, causes the loss of data on the client's computer system or otherwise causes the computer to crash. Nothing is more frustrating to the user than to have five applications open and lose all the data because of a computer crash. Although multitasking systems such as Windows 95, Windows NT, and UNIX are less susceptible to crashing than the Windows operating system was, system crashes still happen.

VBScript prevents virus infections and other potential security and safety problems by eliminating the cause of such problems entirely. First of all, VBScript does not read and write files or databases in the traditional fashion. This approach might seem like quite a limitation, and it is indeed limiting, but it stops up a very large security leak. Damage could come to a user's computer through a Web page that opens and modifies a file or perhaps deletes a file on the user's computer. Therefore, VBScript itself is not able to open or modify any files on the user's computer.

The second area of safety is making sure a VBScript will not cause the computer to crash. If an important exchange of information is happening in a Web page, the user would certainly not want the computer to crash because of an ill-formed script.

The design of VBScript properly reflects the goal to prevent security leaks and ensure safety with VBScript, but the story does not end yet. Although VBScript works with ActiveX controls, intrinsic HTML controls, OCXs, and OLE objects within a Web page, VBScript has no control over what goes on when the code that composes such controls executes. All VBScript "sees" when working with a control or object is the interface that control provides. A control could, for example, modify a file. VBScript doesn't modify the file-the control does. Although VBScript can't be faulted for causing damage to a user's system, it cannot be responsible for either the security or stability of any controls or OLE objects it works with.

If a control were written with some sort of a glitch or bug inside of it, it could cause a computer to crash or, at the very least, cause the Web page to not function properly. Again, the problem does not lie in VBScript itself but in the control it interfaces with. A Web page is only as stable as its least stable component. If a buggy control is included on a Web page, the potential of that Web page to become unstable is equal to the stability of that control. Therefore, it is essential that the Web page designer choose controls that will work as bug free as possible with any browser or any platform on which the Web page is run.

Microsoft's Internet Explorer 3.0 is a good example of the current state of browser security. Internet Explorer contains a feature called Safe Content that makes sure that no unknown programs or components can be downloaded to the user's computer by a Web page without the user's consent. Refer to Figure 21.3 for the Internet Explorer 3.0 Programs dialog box for setting security options for programs you receive within Web pages from the Internet.

Figure 21.3 : Microsoft Internet Explorer 3.0 Programs dialog box.

As you can see, you have the option of choosing "Expert," which warns you about any security problems before a Web page is displayed, "Normal," which automatically prevents security problems by not displaying content that causes security violations, and "None," which displays all contents without worrying about security violations. The Expert setting will inform you whenever an object is required by the Web page or if a scripting language changes the properties of an existing control on a Web page. These safeguards are an example of part of an evolving process of making the Web browsers more secure in an environment of rapidly growing objects, controls, and scripting languages.

You might want to select the None setting while you use the samples on this book's CD-ROM. These samples use Microsoft ActiveX controls only. Because your browser can run these samples locally from the CD-ROM, you don't even have to be on the Internet to use them and can take comfort that you are working in a safe computing environment. Once you go out on the real Internet, however, any page can cause a control to be downloaded and all bets are off! We recommend that you use Expert when visiting pages with which you are not well acquainted. Microsoft has provided a framework for certifying and identifying trusted controls, so you can choose to work with only known-commodity controls.

The Internet is continuing to develop rapidly in the arena of security, particularly in the area of component distribution and security. It is important to understand that security considerations have shaped the capabilities inherent in the VBScript language.

VBScript Versus Visual Basic and Visual Basic for Applications

Many programmers who are interested in VBScript have used either Visual Basic or Visual Basic for Applications. If you are included in that group, you most likely know that VBScript is a subset of Visual Basic. You've probably also heard Microsoft's claim that if you already know Visual Basic or Visual Basic for Applications, you know VBScript. Yes, well-sort of. You know what you can do, but you might not be so sure about what you cannot do.

One of the first striking differences between VBScript and Visual Basic is that Visual Basic has a design-time environment. When you run Visual Basic, you get an attractive editing environment where you can craft forms and write code using an interactive shell. When you work with VBScript, on the other hand, you have no such environment. VBScript code "lives" within an HTML document, which is a plain text file. At the time of this printing, neither Microsoft nor any other large commercial vendor has a design environment that lets users create VBScript code in the way Visual Basic does. This restriction is due, in part, to the way in which VBScript works. Visual Basic code creates Windows applications that operate in and of themselves. On the other hand, VBScript code works inside of HTML documents and runs along with HTML.

Even though VBScript is an interpreted language, as is its parent, you must create VBScript code manually. Some tools already are emerging to overcome this tedious limitation. Microsoft's ActiveX Control Pad, for example, helps overcome the tedious work of inserting controls and editing HTML code by providing an editing and layout tool that is more sophisticated than Notepad. This tool is currently available for free and can be located on the Microsoft Web site, www.microsoft.com/workshop. You can use the tool for automatic insertion of control object definitions and as a layout editor. The layout editor lets you lay out pages interactively, much like Visual Basic 4.0, and stores the results in a separate file for the Layout control to integrate on your page. And the ActiveX Control Pad even provides a Script Wizard that lets you define scripts through an interactive high-level interface, rather than by entering source code statements.

Because the focus in this book is on helping you understand all the details of VBScript, however, we steer clear of the higher level tools that can shield you from some of the underlying details. We want you to learn those details! Therefore, the samples here are presented at the source code level and can be entered with a text editor such as Notepad. After you gain a well-grounded knowledge of VBScript, then you might choose to progress to some of the higher level tools.

The other primary difference between VBScript and Visual Basic, aside from development environments, is the language itself. As mentioned previously, Visual Basic supports many commands, keywords, and data types that VBScript does not support. A summary of the differences between VBScript and Visual Basic for Applications can be found on Microsoft's VBScript Web site, as well as in Teach Yourself VBScript in 21 Days by Brophy and Koets, from Sams.net Publishing. Understanding how to port code from one language to another is very important, and Teach Yourself VBScript in 21 Days clearly explains the process.

How VBScript Enhances Browsers and HTML

VBScript enhances Web browsers in a variety of helpful and significant ways. Web browsers are able to read and interpret HTML code, formatting the text and other data in a Web page based on the specifications of the browser. HTML was originally written to address content of a document while enabling the browser itself to worry about how to present the output to the user. As such, it is a relatively simple language that is quite limited in its power. One of its biggest limitations was the inability to support interaction with a Web page as fully as users have come to expect from computer-run applications. For instance, people who use sophisticated word processors such as Microsoft Word or MacWrite expect the user-friendly atmosphere of menus, toolbars, and dialog boxes that help them accomplish various tasks. When working with a Web page built with HTML alone, the user is very limited in what he or she can do. Various controls can be placed on a form, but the Web page input must be submitted back to the server in order to perform any processing. This restriction usually makes Web pages fairly rigid in their interactive capabilities.

Web pages built entirely on HTML often require the user to set a series of controls, such as check boxes and text fields. Typically an entire form's worth of data is supplied at a time. After filling in fields on a form, the user can click on a button, usually called Submit, and the contents of the Web page are sent back to the server. The only work that the client's computer performs is to display the Web page to the user. The server performs all the intelligent work and processing. Remote processing not only increases the amount of traffic required on the Internet but also prevents the user from interacting with the Web page. If, for example, the Web page was used to take a survey, HTML would not be able to validate the user's input. In other words, the Web page wouldn't be able to make sure that the user was entering valid information. In fact, the user could spend a long time entering invalid data, only to have the server echo the data back to the user and tell him or her to enter it again. Or, worse yet, the server might discard all of the user's data and force him to start over and enter all the data again!

With VBScript, however, the Web page has its own intelligence. VBScript can, for example, make sure that what a user enters is valid before the user "submits" it to the server. Local validation eliminates a great deal of extra traffic, not to mention the delay the user must experience in waiting for the information to get to the server. One immediate benefit for the user is that faster, more responsive Web pages make the overall Web page experience more enjoyable. Furthermore, many effects, such as multimedia and animation, that are limited, if not nonexistent, in HTML, are relatively easy to implement in VBScript, and they can also enhance the Web page experience.

Another great strength of VBScript is its capability to act as a "glue tool" that can integrate and control all the components on a Web page, such as OLE objects, ActiveX controls, Java applets, Intrinsic HTML form controls, and Virtual Reality Modeling Language (VRML) controls.

NOTE
VRML is a language used to present a three-dimensional, graphical world to the Web user

This same capability also made Visual Basic the important tool it is today. Therefore, VBScript enhances the power of browsers and HTML because it gives the Web page a higher degree of interaction with the user, allows for processing on the user's computer rather than only on the server, provides the user with more control over the Web page, and glues together various components of a Web page.

Host Environments

VBScript can be supported and used in any browser distributed with VBScript runtime support, as well as with a variety of other tools and components. This section briefly reviews some of the host environments and then turns to the specifics of how to make VBScript work in HTML browsers.

Web Browsers

At the time of this writing, the Internet Explorer browser supported VBScript. Other browsers might add support as well, as VBScript gains enough popularity that market forces and user pressures force the issue. Add-on components already exist to extend VBScript support to Netscape's Navigator browser. Because this is currently the browser with the largest market share, there is hope in many quarters that Netscape might add native support for VBScript. While to date there has been no indication this is planned, if and when that move occurs, VBScript will become virtually a de facto standard in the browser arsenal and would likely be a part of all serious browsers.

Microsoft's Internet Explorer is the focus of the discussion in the remainder of the chapters of this book relating to VBScript because it is the most established VBScript platform. If you use VBScript in another environment, recognize and take comfort in the fact that VBScript should work essentially the same everywhere. No matter which browser your script runs in, you can expect consistent behavior from the core language. The runtime interpreter, the engine that interprets and processes the language, will also behave the same because it will be derived from the same Microsoft VBScript source code wherever it is found.

If a browser or tool vendor ports VBScript to a different environment, the vendor can license the source code free of charge from Microsoft and modify it to work in that other environment. But even the modified version must meet Microsoft's criteria for VBScript conformance. However, you might find slight variations in the objects that a host, such as a browser, makes available to VBScript to manipulate. Aside from these differences, though, the language and behavior of VBScript will be consistent from one implementation to another.

Microsoft's Internet Explorer already contains the Visual Basic runtime interpreter. Versions for 16- and 32-bit Windows will become available, as well as versions for the Macintosh and UNIX-based computers. This cross-platform support of VBScript through the Internet Explorer will not restrict VBScript users to the Windows environment, as did Visual Basic and Visual Basic for Applications. Furthermore, Microsoft will provide the Internet Explorer without charge to users across these platforms.

So what do we mean when we say that a browser supports VBScript? When we answer this question later, you will understand the mechanics of running a VBScript within a browser. But first, we need to consider some non-browser environments that also host VBScript.

Other Internet Tools

In addition to Web browsers, a variety of other tools for the Internet will benefit from VBScript support. The first case to consider is custom controls, which can be used as building blocks for standard applications. A variety of companies produce browser controls for Windows applications, for example, that enable developers to build their own Web browsers or incorporate some browser functionality into their applications. If, for example, a designer is building an application for a company, she might want to make the application "Internet aware" and provide a browser interface to the World Wide Web from within the application itself. This type of browser component has the potential to support VBScript code as well, thereby giving application developers who incorporate browsers into their own applications the ability to support Web pages that use VBScript.

Some services not directly related to Web browsers also support VBScript. Microsoft's Internet Information Server product is one example. The Internet Information Server enables computers where Web pages permanently reside to share their pages with the rest of the Internet. The product works in conjunction with Windows NT to deliver Web pages across the Internet in response to a user's request. VBScript is even used to glue server-side solutions together, just as it can be used to glue Web page components and logic together.

VBScript support has also been projected for WebObjects, a dynamic Web page tool from Next Software, Inc. You can use WebObjects for rapid development of sophisticated Web server solutions. Another area where VBScript can be used is Microsoft's Active VRML environment. VRML is quickly becoming very popular on the World Wide Web because it enables designers to create a three-dimensional world, rather than fixed Web pages. The user can then navigate through this world, much like a person would walk through a house, to explore its contents. In the early alpha models of Microsoft's ActiveVRML technology, VBScript can interact with Microsoft's Active VRML standard events and properties of an Active VRML viewer ActiveX control. The ActiveVRML technology is still evolving, but if this level of support remains, VBScript will help enhance the power of VRML and give the Web user even more visually appealing ways to interact with information.

Why is VBScript so prevalent? Precisely because it was intended to be. Microsoft designed VBScript to be a general-purpose scripting language. It was not intended to be constrained just to HTML Web pages. To the contrary, it is specified in such a way that it can easily be incorporated as a smart programming language into any application. Microsoft provides the VBScript runtime license and source code free of charge to any software manufacturer that wants to make its product "VBScript-aware."

Assume, for example, that you are marketing your own spreadsheet software. You want to give your customers a way to write macros that can interact with the contents of a spreadsheet based on instructions provided by the customer. One of the best ways to accomplish this task is to incorporate VBScript runtime support. Then you've extended the capabilities of your product by giving your customers an easy way to control the spreadsheet. It costs you, the manufacturer, virtually nothing, because you were able to leverage Microsoft code. Models of this sort will continue to facilitate the growth of VBScript into arenas beyond the Web page.

Microsoft announced plans to license its more extensive Visual Basic engine, called VBA or Visual Basic for Applications, a few months after announcing the licensing plans for VBScript. Software providers can incorporate VBA into their product in much the same way as they can incorporate VBScript. The difference is that VBA is a high-end product with more function, options, and bulk. VBA 5 has a development environment that can be called from the application that incorporates it, for example. VBScript, on the other hand, is designed to be lightweight, fast, and simple. Because it is derived from VBA, and is in fact a proper subset of VBA, the languages are very similar. Which will most vendors incorporate? Time will tell. Given that VBScript can be licensed for free, which is not the case with VBA, you should expect to see VBScript in more and more applications.

As these examples show, the applications for VBScript are potentially broad. In addition, VBScript can be targeted for networks other than the World Wide Web. It can also be useful in intranet applications, in a sense running over a private mini-Web.

NOTE
An intranet is a scaled version of the Internet that is contained within an organization-an intranet's corporation is equivalent to the Internet's world.

VBScript technology can be used equally well in either an intranet or Internet environment. In fact, VBScript can be especially well suited to the intranet because it can craft very sophisticated solutions on a powerful set of front-end web pages and corresponding customer-made business rules as determined by the organization. Distribution and support of components might be easier to handle when those components are shared just within the organization, rather than with the entire Internet. VBScript could make such a strategy more feasible.

The possibilities for using VBScript are numerous. Still, by far the most common use of VBScript today is as a tool to create great Web pages. In this and the next two chapters, VBScript is demonstrated within the Web browser. Although we have restricted our discussion to preliminary versions of Microsoft's Internet Explorer 3.0 running within Windows 95, you can apply the concepts you learn here in any of the platforms or within any of the tools that support VBScript. Our next task is to show you how a browser supports VBScript. Then you will be ready to create your first VBScript program!

Placing VBScript Code Within an HTML Document

In order to place code from a scripting language into an HTML document, you must use script tags. The script tag looks like this:


<SCRIPT>

It is closed using this:


</SCRIPT>

Unlike the other tags you have seen so far, however, the opening script tag requires an argument-namely, the language of the script. The opening script tag embeds VBScript code within an HTML document. For VBScript code, the tags are used as follows:


<SCRIPT LANGUAGE="VBScript">

...your VBScript code goes here

</SCRIPT>

The language parameter should be specified as "VBScript", including the quotation marks. Early beta versions of Internet Explorer used VBS without quotation marks, but later betas and the final release require the parameter as specified above. If you are using another scripting language, such as Java, you need to enter an indicator such as "JavaScript" rather than "VBScript" in the language argument when you write the opening script tag. When the browser encounters a script tag, it checks to see what language the script has been written in. Then the browser checks whether an interpreter is available that can read the script code. If so, the interpreter takes over. If not, the script tag is ignored and HTML moves on its merry way to the next line of code after the script tag.

You can also use a variety of other tags in an HTML document. Some of these tags are introduced in later chapters because they are important in working with VBScript. Others are not directly applicable to VBScript but are useful to know anyway. Again, you might want to read Teach Yourself Web Publishing with HTML 3.0 in a Week or Teach Yourself More Web Publishing with HTML 3.0 in a Week, both from Sams Publishing, for more information on how to use HTML in building Web pages.

Now that you have all the pieces of the puzzle, let's put them together so you can see how to place VBScript code in your Web pages. To place a script inside an HTML document, you should use the script and comment tags together. As previously mentioned, the comment tags are important just in case someone reads the Web page with a browser that does not support VBScript. You will see an example of the impact in a moment.

The following is the syntax for placing VBScript code inside an HTML document. You can place the code anywhere within the framework of your HTML document.


<SCRIPT LANGUAGE="VBScript">

<!--

      ... your VBScript code goes here

-->

</SCRIPT>

Notice that the comment tag appears immediately after the script tag so that if the browser doesn't recognize the script, it will ignore the script tag and treat anything that follows as regular HTML text. The comment tags prevent the browser from displaying the script code just in case it is a browser that doesn't recognize VBScript. The VBScript interpreter will not process the comment tags because when the browser finds an HTML comment, it does not bother to pass the comment on to the VBScript interpreter. As a result, you don't need to worry about VBScript not being able to figure out why the HTML comment tags are present.

Internet Explorer lets you place VBScript code anywhere inside your HTML document. In practice, however, the best place for your VBScript code is at the very end of the body section in your HTML document. This gives you an easy frame of reference to locate all code in the page.

Also, you can place VBScript code within control definitions themselves. If you use this technique within the body of the HTML document, VBScript must also reside in the body of the document. Placing the script code at the end of the body section is, therefore, the safest choice.

Listing 21.1 shows an example of a Web page that contains VBScript code placed at the end of the body section of the document.


Listing 21.1. A Web page using VBScript code.

<HTML>

<HEAD>

<TITLE>VBScript Test Page</TITLE>

</HEAD>



<BODY>



<H1>

<A HREF="http://www.mcp.com"><IMG  ALIGN=MIDDLE

SRC="../shared/jpg/samsnet.jpg" BORDER=2 HSPACE=20></A>

<EM>VBScript Test Page</EM></H1>



<HR>



<CENTER><INPUT TYPE=BUTTON LANGUAGE="VBScript"

VALUE="Click Here for a Message!" NAME="TestButton"></CENTER>



<HR>



<center>

Adapted From <em>Teach Yourself VBScript in 21

Days</em><br> by <A HREF="../shared/info/keith.htm">Keith Brophy</A>

and <A HREF="../shared/info/tim.htm">Tim Koets</A><br><br>

Return to <a href="VBSDemo.htm">Content Overview</A><br>

Copyright 1996 by SamsNet<br>

</center>



<SCRIPT LANGUAGE="VBScript">

<!--

       Sub TestButton_OnClick()

           MsgBox "It's easy to create message boxes with VBScript!"

       End Sub

-->

</SCRIPT>



</BODY>



</HTML>


This Web page is shown in Figure 21.4.

Figure 21.4 : An example of a Web page using VBScript.

As you can see, the Web page includes a button that the user clicks to display a message box (which is also shown in Figure 21.4). The code used to create the Web page in Figure 21.4 works fine because the browser recognizes VBScript. If the browser wasn't VBScript-aware, the comments between the script tags would prevent the code from being displayed in the Web page.

To see what would happen if you didn't use those comment tags and then used a browser that was not VBScript-aware, look at the Web page created in Listing 21.2 and shown in Fig-ure 21.5.


Listing 21.2. A Web page using VBScript code and no comment tags for the script code.

<HTML>



<HEAD>

<TITLE>VBScript Test Page</TITLE>

</HEAD>



<BODY>



<H1>

<A HREF="http://www.mcp.com"><IMG  ALIGN=MIDDLE

SRC="../shared/jpg/samsnet.jpg" BORDER=2 HSPACE=20></A>

<EM>VBScript Test Page</EM></H1>



<H2>No comment markers!</H2>



<HR>



<CENTER>

<INPUT TYPE=BUTTON LANGUAGE="VBScript" VALUE="Click Here for a Message!" 

NAME="TestButton"></CENTER>



<HR>



<center>

Adapted From <em>Teach Yourself VBScript in 21

Days</em><br> by <A HREF="../shared/info/keith.htm">Keith Brophy</A>

and <A HREF="../shared/info/tim.htm">Tim Koets</A><br><br>

Return to <a href="VBSDemo.htm">Content Overview</A><br>

Copyright 1996 by SamsNet<br>

</center>



<SCRIPT LANGUAGE="VBScript">

       Sub TestButton_OnClick()

           MsgBox "It's easy to create message boxes with VBScript!"

       End Sub

</SCRIPT>



</BODY>



</HTML>


Figure 21.5 shows the Web page when viewed with Mosaic, which at the time of this printing did not support VBScript.

Figure 21.5 : An example of VBScript code with no comment tags.

Notice that in this case all the VBScript code is printed in the Web page-a very undesirable result! You can avoid this misfortune by using the comment tags whenever you write VBScript code within a Web page. That's all there is to it!

Summary

This chapter introduced you to the exciting world of VBScript. You learned what VBScript is, what it can do, and how it resembles its parent language, Visual Basic for Applications. You also learned about the tools you need to incorporate VBScript into your Web pages and that VBScript is an easy language to learn. Because security is a big issue on the Internet these days, we also discussed the ways in which VBScript is secure on the Web. Finally, you learned how to place VBScript code within an HTML document. This relatively easy process enables you to work with any browser that supports HTML and VBScript.

The next chapter presents a comprehensive overview of the VBScript language, and you will learn to write VBScript-based Web pages on your own. Then, in Chapter 22, you can examine several useful, working Web pages that take advantage of VBScript and the rich suite of capabilities it offers.