home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Platinum Edition Using HTML 4, XML, and Java 1.2
(Publisher: Macmillan Computer Publishing)
Author(s): Eric Ladd
ISBN: 078971759x
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


CHAPTER 33
Active Server Pages and VBScript

by Ramesh Chandak, Eric Ladd, and Jim O'Donnell

In this chapter
Introduction to VBScript 836
VBScript Identifiers 836
Objects, Properties, Methods, and Events in VBScript 837
VBScript Language Elements 838
Testing Conditions in VBScript 843
Using Other VBScript Statements 845
VBScript Functions 847
Active Server Pages and the Active Server Platform 848
Building Active Server Page Applications 850
Active Server Page Objects 852
Using Existing ASP Components 861
Browser Capabilities Component 868
Developing ASP Applications with Visual InterDev 869
Putting It All Together: A Simple ASP Application 870

Introduction to VBScript

Like JavaScript, Microsoft’s Visual Basic Scripting Edition (VBScript) enables you to embed commands into an HTML document. When a user of a compatible Web browser (for example, Internet Explorer or Netscape Navigator with the ScriptActive plug-in from Ncompass Labs) downloads your page, your VBScript commands are loaded by the Web browser along with the rest of the document and are run in response to any of a series of events. Again, like JavaScript, VBScript is an interpreted language; Internet Explorer interprets the VBScript commands when they are loaded and run. They do not first need to be compiled into executable form by the Web author who uses them.

VBScript is a fast and flexible subset of Microsoft’s Visual Basic and Visual Basic for Applications languages and is designed to be easy to program in and quick in adding active content to HTML documents. The language elements are mainly ones that should be familiar to anyone who has programmed in just about any language: If…Then…Else blocks and Do, While, and For…Next loops, and a typical assortment of operators and built-in functions. The first half of this chapter takes you to the heart of the VBScript language and shows you examples of how to use it to add interaction and increased functionality to your Web pages. Then, in the second part of the chapter, you’ll learn how to use VBScript on the server side to create applications using Active Server Pages.

VBScript Identifiers

An identifier is a unique name that VBScript uses to identify a variable, method, or object in your program. As with other programming languages, VBScript imposes some rules on what names you can use. All VBScript names must start with an alphabetic character and can contain both uppercase and lowercase letters and the digits 0 through 9. They can be as long as 255 characters, although you probably don’t want to go much more than 32 or so for readability’s sake.

Unlike JavaScript, which supports two ways for you to represent values in your scripts, literals and variables, VBScript has only variables. The difference in VBScript, therefore, is one of usage. If you wish to use a constant value in your VBScript programs, set a variable equal to a value and don’t change it. This discussion will continue to refer to literals and variables as distinct entities, although they are interchangeable.

Literals and variables in VBScript are all of type variant, which means that they can contain any type of data that VBScript supports. It is usually a good idea to use a given variable for one type and explicitly convert its value to another type as necessary. The following are some of the types of data that VBScript supports:

  Integers—These types can be 1, 2, or 4 bytes in length, depending on how big they are.
  Floating point—VBScript supports single- and double-precision floating point numbers.
  Strings—Strings can represent words, phrases, or data, and they are set off by double quotation marks.
  Booleans—Booleans have a value of either true or false.
  Objects—A VBScript variable can refer to any object within its environment.

Objects, Properties, Methods, and Events in VBScript

Before you proceed further, you should take some time to review some terminology that may or may not be familiar to you. VBScript follows much the same object model followed by JavaScript and uses many of the same terms. In VBScript, as in JavaScript—and in any object-oriented language for that matter—an object is a collection of data and functions that have been grouped together. An object’s data is known as its properties, and its functions are known as its methods. An event is a condition to which an object can respond, such as a mouse click or other user input. The VBScript programs that you write make use of properties and methods of objects, both those that you create and those objects provided by the Web browser, its plug-ins, ActiveX Controls, Java applets, and so on.


The following is a simple guideline: An object’s properties are the information it knows; its methods are how it can act on that information; and events are what it responds to.


NOTE:  A very important, and a little confusing, thing to remember is that an object’s methods are also properties of that object. An object’s properties are the information it knows. The object certainly knows about its own methods, so those methods are properties of the object right along with its other data.

Using Built-In Objects and Functions

Individual VBScript elements are objects. Literals and variables are objects of type variant, which can be used to hold data of many types. These objects also have associated methods—ways of acting on the different data types. VBScript also enables you to access a set of useful objects that represent the Web browser, the currently displayed page, and other elements of the browsing session.

You access objects by specifying their names. The active document object, for example, is named document. To use document’s properties or methods, you add a period (.) and the name of the method or property you want. For example, document.title is the title property of the document object.

Using Properties

Every object has properties, even literals. To access a property, use the object name followed by a period and the property name. To get the length of a string object named address, you can write the following:

address.length

You get back an integer that equals the number of characters in the string. If the object you are using has properties that can be modified, you can change them in the same way. To set the color property of a house object, just write the following:

house.color = “blue”

You can also create new properties for an object by naming them. Assume, for example, that you define a class called customer for one of your pages. You can add new properties to the customer object as follows:

customer.name = “Jim O’Donnell”
customer.address = “1757 P Street NW”
customer.zip = “20036-1303”


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.