Microsoft Visual Basic Scripting Edition: Frequently Asked Questions

Microsoft Corporation

September 1996
Last Updated: Fall 1997

Contents

What is Visual Basic Scripting Edition?

How do I get Visual Basic Scripting Edition?

Where can I find Visual Basic Scripting Edition documentation?

What support is available for Visual Basic Scripting Edition?

How does Visual Basic Scripting Edition compare to Visual Basic and the Visual Basic language in the Microsoft Office applications?

How does Visual Basic Scripting Edition compare to JavaScript and Java?

What platforms will support Visual Basic Scripting Edition?

Can I use Visual Basic Scripting Edition as a scripting language for my own application?

What objects, methods, properties, and events can I use?

How can I write HTML text to the window?

Where should my script be within the document?

How do I execute a script when the user clicks on text or a picture?

How can I change the content of another frame?

How can I get the value of an object or variable in another frame?

How can I access an object in another frame?

How can I reference objects or variables in a layout control?

How can I test or set which radio buttons are selected?

Why do I get JavaScript errors for my Visual Basic Scripting Edition code or vice versa?

How can I call a Visual Basic Scripting Edition function from an anchor?

How can I prevent a form from being submitted if it's invalid?

How can I debug Visual Basic Scripting Edition code?

What is Visual Basic Scripting Edition?

Microsoft® Visual Basic® Scripting Edition (also known as VBScript) is a subset of the Visual Basic language. It is implemented as a fast, portable, lightweight interpreter for use in World Wide Web browsers and other applications that use ActiveX™ controls, OLE Automation servers, and Java applets.

How do I get Visual Basic Scripting Edition?

Visual Basic Scripting Edition is currently only available as part of Microsoft Internet Explorer and Internet Information Server. Both Microsoft Internet Explorer version 4.01 (http://www.microsoft.com/ie/) and Microsoft Internet Information Server version 4.0 (http://www.microsoft.com/iis/default.asp) are available through the Microsoft Web site.

Where can I find Visual Basic Scripting Edition documentation?

Documentation is available on the Visual Basic Scripting Edition Web page (http://www.microsoft.com/vbscript/). This page may be updated frequently, so check back often.

What support is available for Visual Basic Scripting Edition?

There are a variety of support options (see http://www.microsoft.com/SUPPORT/). Be sure to join the mail lists where Visual Basic Scripting Edition and other components of the new Internet technology are discussed.

How does the Visual Basic Scripting Edition compare to Visual Basic and the Visual Basic language in the Microsoft Office applications?

Visual Basic Scripting Edition is a strict subset of the Visual Basic for Applications language used in Microsoft Excel, Microsoft Project, Microsoft Access, and the Visual Basic 5.0 development system. Visual Basic Scripting Edition is designed to be a small and lightweight interpreted language, so it does not use strict types (only variants). Also, because VBScript is intended to be a safe subset of the language, it does not include file I/O or direct access to the underlying operating system. You can find a complete list of the differences between VBScript and Visual Basic for Applications on the "Visual Basic for Applications Features not in VBScript" Web page (http://www.microsoft.com/VBSCRIPT/US/VBSLANG/VSGRPNONFEATURES.HTM).

How does Visual Basic Scripting Edition compare to JavaScript and Java?

When used in Internet Explorer, Visual Basic Scripting Edition is directly comparable to JavaScript (not Java). Like JavaScript, VBScript is a pure interpreter that processes source code embedded directly in the HTML. Visual Basic Scripting Edition, like JavaScript, does not produce stand-alone applets but is used to add intelligence and interactivity to HTML documents. For the programmers who already know Visual Basic, the Visual Basic Scripting Edition is a valuable alternative to JavaScript in activating Web pages.

What platforms will support the Visual Basic Scripting Edition?

The Visual Basic Scripting Edition is available or under development for Microsoft Windows® and Windows NT® (including native versions for Alpha), 16-bit Windows, and Macintosh®. Microsoft is working with third parties to provide UNIX versions for Sun Microsystems, Hewlett-Packard, Digital Equipment Corporation, and IBM platforms.

Can I use Visual Basic Scripting Edition as a scripting language for my application?

Yes. If you write it to support ActiveX scripting, your application can host Visual Basic Scripting Edition and users of your application can use VBScript. Another important bonus is that because ActiveX scripting is an open standard, your application can host any other language that is written to that standard. You must acknowledge the use of Microsoft technology and include the appropriate trademark and copyright information, but you can use and distribute Visual Basic Scripting Edition free of royalties.

What objects, methods, properties, and events can I use?

There are three separate classes of objects available within Visual Basic Scripting Edition:

The Visual Basic Scripting Edition engine provides the core runtime functionality—a subset of the full Visual Basic language—including a minimal set of basic objects. The vast majority of objects used in scripting is provided by Internet Explorer. In general, anything that is specific to the Internet is provided by Internet Explorer, and anything that is generally useful is provided directly in Visual Basic Scripting Edition. The Web author can insert additional objects through the <OBJECT> HTML tag.

The most complete documentation of the objects, methods, events, and properties available in Internet Explorer are available in the ActiveX SDK in the Object Model for Scripting section of the SDK Overview. The SDK can be downloaded from http://www.microsoft.com/intdev/sdk/.

This material can also be found through ActiveX Control Pad, a new authoring tool created by Microsoft. Select the Script Wizard while in the HTML view. The Object/Action view on the right then displays the Window object. Unfold it to reveal the complete object model beneath. Under Tools/Options, you can select code as the default view in the Script Wizard's bottom pane. Control Pad can be downloaded from http://www.microsoft.com/workshop/author/cpad/.

How can I write HTML text to the window?

You can use the Document.write method to write any text, HTML or otherwise, to the window. These commands must be executed before the document has finished loading. The best way is to execute Visual Basic Scripting Edition commands that are inline, not subroutines or functions that are triggered by events.

Example:

<HTML>
<HEAD><TITLE>Dynamic Greeting Sample</TITLE>
<SCRIPT LANGUAGE="VBScript">
<!-- OPTION EXPLICIT
If Hour(time) < 6 then
document.write "<b>Good grief!</b>"
Else if Hour(time) < 12 then
document.write "<b>Good morning!</b>"
Else if Hour(time) < 17 then
document.write "<b>Good afternoon!</b>"
Else document.write "<b>Good evening!</b>"
End If
-->
</SCRIPT>
</HEAD><BODY>
<p>This is a sample document</p>
</BODY></HTML>

Where should my script be within the document?

The very bottom of the HTML document seems to be best in most cases, and keeping it separated from the rest of the HTML makes it clearer. In dynamic HTML scenarios, where the script is creating some of the text displayed on the page, the script must be at the beginning or embedded in the HTML.

How do I execute a script when the user clicks on text or a picture?

Write your anchor as:

<A HREF="" language=VBScript onclick="alert 'got here' "> <IMG SRC=some.gif> </A> or use VBScript as...
<A HREF="" onclick="DoStart"> Click me!</A>
<SCRIPT Language = "VBScript">
<!--
SUB DoStart
alert "got here"
END SUB
-->
</SCRIPT>

How can I change the content of another frame?

In a simple window, containing one frameset and multiple frames, you can use:

top.framename.location.href="newfile.htm"

—or—

top.frames(n).location.href="newfile.htm"

where n is an index starting from 0 in the order the frames are defined within the frameset, and framename is the value of the NAME attribute within the <FRAME> tag.

In a complex page containing frames nested within frames, the frame name is built based on the hierarchy of nested frames within the page. The first portion of the name is the starting point, either top to start from the top level of the window, or parent to start relative to the frame where the script is executed. Multiple parent references can be used to go up through additional levels of framesets if necessary. Then you use the name or indexed reference to the proper frame within each level.

For example, a window containing a banner frame across the top, a navigation bar under it on the left, and the remainder showing subject contents might be defined as:

<FRAMESET ROWS="80, *">
<FRAME NAME=banner, SRC="banner.gif">
<FRAMESET COLS="120, *">
<FRAME NAME=navbar, SRC="nav1.htm">
<FRAME NAME=contents, SRC="initial.htm">
</FRAMESET>
</FRAMESET>

Scripts in the navigation bar could change the Contents frame with:

parent.frames(1).location.href="newfile.htm"

—or—

parent.contents.location.href="newfile.htm"

The banner could be changed with:

top.banner.location.href="banner2.gif"
parent.parent.frames(0).location.href="banner2.gif"

All floating frames are part of the document object, which is within the appropriate frame if framesets are used. Floating frames can be referenced by name or by index number within the containing document. To change a floating frame within the same regular frame, use:

document.framename.location.href="newfile.htm"

To change a floating frame in a different frame, use the above procedure to identify the frame and add the "document" object reference as in:

top.frames(1).contents.document.frames(0).location.href="newfile.htm"

How can I get the value of an object property or variable in another frame?

Use top.framename.varname or parent.framename.varname. To get the value of a control property use top.framename.control.property. See the previous item for a complete description of referencing frames.

How can I access an object in another frame?

top.framename.objectID (or parent...)
example: parent.controls.Axa1.FireImportedEvent 101

How can I reference objects or variables in a layout control?

You can access the objects that are in the layout from the HTML and vice versa, but if you use the Control Pad Script Wizard it won't show you the possibilities. You pretty much have to code this by hand.

To reference the contents of the ALX file from the HTML file, you use the following notation:

<LayoutControlID>.<ControlID>.<PropertyOrMethod>
<LayoutControlID>.<GlobalProcedureOrVariable>

Where <LayoutControlID> is the ID you assigned to the layout control in the HTML file.

To reference the contents of the HTML file from the ALX file, you use the following notation:

window.<ControlID>.<PropertyOrMethod>
window.<GlobalProcedureOrVariable>

The object tag for the ALX that is located in the HTML file has an ID string that will be something like Layout1_ALX. Use this ID tag for connecting between separate .alx layouts. (This ID should not be the same ID that shows in the Property Window for the layout control.) For example:

Layout1_ALX.textbox1.text="Hello World"

Note   A script in an HTML file cannot call a VBScript procedure located in an .alx file, but a script in an .alx file can call a procedure in an HTML file.

Be careful not to repeat control names from one .alx layout to another. If Layout1 has a textbox1, then Layout2 should not. Otherwise, things happen unpredictably.

How can I test or set which radio buttons are selected?

Define the buttons like this:

<FORM NAME="InputForm">
<INPUT TYPE=RADIO NAME="ImageSet">Image Set 1
<INPUT TYPE=RADIO NAME="ImageSet">Image Set 2
<INPUT TYPE=RADIO NAME="ImageSet">Image Set 3
<INPUT TYPE=RADIO NAME="ImageSet">Image Set 4
</FORM>

Then you can initialize any or all buttons with:

InputForm.ImageSet.Item(n).Checked = TRUE

(where n starts from 0)

And test values with:

If InputForm.ImageSet.Item(0).Checked Then...

Why do I get JavaScript errors for my Visual Basic Scripting Edition code or vice versa?

Unless told otherwise, Internet Explorer assumes that the code is Micorosoft JavaScript™. You can fix this in several ways:

How can I call a Visual Basic Scripting Edition function from an anchor?

There are two methods available:

How can I prevent a form from being submitted if it's invalid?

The easiest and clearest way is to execute the Submit method only if everything is okay:

<FORM NAME="TestForm">
Firstname: <INPUT NAME="FirstName" VALUE="" MAXLENGTH="50" SIZE=50>
<INPUT TYPE="BUTTON" VALUE="Run Query" NAME="RQ3">
</FORM>

<SCRIPT LANGUAGE="VBScript">
Sub RQ3_OnClick
Dim MyForm
Set MyForm=Document.TestForm
If RTrim(MyForm.FirstName.Value)="" then
MsgBox "You must enter a Firstname", 64, "Missing information!"
Else
TheForm.Submit
End if
</SCRIPT>

How can I debug Visual Basic Scripting Edition code?

We highly recommend using Visual Basic 5.0 as a test tool for VBScript code. Since Visual Basic Scripting Edition is a subset of Visual Basic, it's really easy to move code back and forth.

You can use the HTML intrinsic controls as OCXs in Visual Basic 5.0 forms. The reason this works is that this is exactly the way Internet Explorer implemented them. To add these to your tool palette, use "Tools\Custom controls" and then check "Microsoft HTML Intrinsic Controls."

When you're programming in Visual Basic 5.0, you'll of course have to stick to the Visual Basic Scripting Edition subset in order to allow you to move code to Visual Basic Scripting Edition and have it work. Here are the normal gotcha's:

Using these techniques, you can do most of your code development in Visual Basic, which has great debugging tools. Usually I work out coding problems in Visual Basic and then immediately copy all of the code and paste it into the <SCRIPT> . . . </SCRIPT> block in my HTML file and hit the refresh button and verify that everything works okay. (And, yes, if I follow the rules above, it nearly always does!) An integrated environment to debug and author VBScript would be great, but this technique works so well that often I don't miss an IDE at all!

When debugging code that can't use this technique, insert Alert statements throughout the code to keep track of progress and the value of important variables or properties.