c-- styles for logos and headline links do not modify internet, red, or black styles -->

Intranet Journal   Earthweb  
Events Jobs Premium Services Media Kit Network Map E-mail Offers Vendor Solutions Webcasts

   Intranet Journal Subjects
Search Earthweb

Privacy Policy



internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

internet commerce
Be a Commerce Partner
















 

[ Home | Discussion Forum | How Do I... | Lotus Notes Intranets | Microsoft SharePoint | Products | Shopping  ]

free news!

VB Script From Step One
From Web Developer's Guide to JavaScript and VBScript
By Peter Aitken

Before I present the nuts and bolts of VBScript, I want to be sure that we're basically on the same page. Of those reading this column, some of you will be experienced Visual Basic programmers; others will know little or nothing about programming. To make things more difficult, VBScript itself is a work in progress, a language rich with promise -- and unfinished features. I’ll do my best to meet the needs of a diverse audience.

For those of you new to Basic, I'll cover the essentials. That will no doubt bore some experts, but I strongly suggest you don’t skip even the basic parts, because there are many ways – some subtle, others not so subtle – in which VBScript differs from its bigger namesake.

In addition, I make freuent reference to JavaScript in the sections below; particularly in those areas where the rival scripting languages differ significantly. I’ll make a special effort to point out tasks that VBScript but not JavaScript can do, or vice versa.

 

An Overview

Perhaps the most important difference between these two scripting languages is the extent to which they rely on objects. As you have seen, JavaScript is a strongly object-based language that relies on objects and their attendant methods and properties for a great deal of its functionality. In contrast VBScript – at least in its current incarnation—relies much less upon objects and much more on built-in functions. To illustrate, let’s take a quick look at how these two languages deal with strings.

JavaScript has the built-in string object. Whatever text data exists in a JavaScript program -– for example, in a named variable -– a string object automatically exists. You can then use this object’s methods and properties to manipulate and obtain information about the string. The following JavaScript code creates a variable named buffer and loads it with the text "JavaScript."

Var buffer = "javascript"

Now, you can use the strings object’s length property to determine the length of the text:

Len = buffer.length

Similarly, you can call upon the string object’s charAt() method to extract a single character from a specified position in the string:

Char = buffer.charAt(5)

VBScript permits the same types of string operations, but uses built-in functions instead. Here is the VBScript code to create a variable and initialize it to "VBScript":

Dim buffer
Buffer = "vbscript"

To obtain the length of the string, call the Len() function:

Length = Len(buffer)

Likewise, to extract a single character from the string, we use the Mid() function:

Char = Mid(buffer, 5, 1)

Is one approach better than the other? You will hear a lot of programmers preach the object-oriented gospel, claiming that any programming technique that is object-based is automatically superior to anything else. In certain programming situations, much can be said for this point of view. In the case of scripting languages, however, it does not necessarily hold true.

Indeed, object-oriented programming languages are superior for large, complex programming tasks. If I were writing a sophisticated statistical analysis program, I would tend to choose an object-oriented language such as C++ over a non object-oriented language, such as FORTRAN. However, for the small, relatively simple programs JavaScript and VBScript were designed to create, the benefits of object-orientation largely fade away. At the very least, the benefits are offset by the greater complexity of thinking in objects. For most rapid development tasks on web sites, JavaScript's object flavor does not convey a telling advantage.

 

The Structure of VBScript Program

In many ways a VBScript program is like a JavaScript program, at least in terms of how it fits in an HTML file. Procedures, defined as discreet blocks of code that have been assigned a name, are placed in the file header, the section between the <HEAD> and </HEAD> tags. Other non-procedure code is placed in the file body. Also, VBScript code must be enclosed in HTML comment tags to prevent it from being displayed in browsers that do not support VBScript. The basic structure of an HTML file with VBScript is as follows:

<HTML>
<HEAD>
<script language – "VBS"
<!-
VBScript code goes here
->
</script>
 
</HEAD>
<BODY>
<script  language – VBS">
<!-
VBScript code goes here too.
->
</script>
</BODY>
</HTML>
</script>
 

VBScript and Visual Basic

Many programmers who are starting to use VBScript to enhance their Web pages have had previous experience with the "big" Visual Basic. How much of that experience can be transferred to VBScript? The answer is, "Quite a lot." Remember that VBScript is a subset of Visual Basic, so nearly everything in VBScript is part of Visual Basic. The major difference lies in what is lost: a significant number of things included in Visual Basic are not a part of VBScript.

------sidebar-----The Visual Basic Family

The Visual Basic product line actually has three members. At the top is Visual Basic 4.0, a complete applications development tool with powerful client/server capabilities that let you create distributed applications. Next is Visual Basic for Applications, Microsoft’s Windows application script language. This is a subset of Visual Basic 4.0, lacking client/server data access, distributed computing, and team source code control. Finally, we have VBScript, a subset of Visual Basic for Applications, designed specifically for Internet use.---end side bar

 

For those of you familiar with Visual Basic, it may be useful to look at some of the differences. The following table describes some of the more important differences between these two versions of Basic. You can see that many of Visual Basic’s capabilities have been omitted from VBScript. This is not to imply that VBScript is inferior or crippled in any way. Remember that VBScript was designed for a specific task. Potentially unsafe capabilities such as file access have been purposely omitted, and other features, such as Visual Basic’s rich set of data types, have been omitted for sake of simplicity.

Differences between VBScript and Visual Basic

Category

In VBScript

In VBA but not in VBScript

Arrays

Dim, Static, ReDim, Erase

Option Base, Lbound <>0

Calling DLL’s

No

Yes

Control Flow

Do…Loop

For…Next

For Each…Next

While…Wend

If…Then…Else

DoEvents

GoSub…Return

GoTo

Line numbers and labels

On Error

Data Types

Variant

All Others (Boolean, Integer, etc.)

Error Trapping

On Error Resume

Next

Err Object

Erl, Error, Error$

On Error…Resume

Resume, Resume Next

File Operations

None

Open, Write #, etc.

Graphics

None

Circle, Line, Pset, etc.

Structures

No

Type…End Type

Creating Classes

No

Dim x as New…

Set x = New…

With … End With

 

ActiveX Controls and Java Applets

You have doubtless heard about Java applets and ActiveX controls, particularly how they are supposed to revolutionize the way we use the Web. How do these sophisticated objects (Microsoft prefers the term 'components') relate to VBScript? Both ActiveX and Java were designed for the same purpose -— providing additional functionality to Web users -- but they go about it in different ways. First, let’s take a look at how they are supposed to function, then we will see how they are different.

People who create Web pages are striving to provide more and more value to their users. It is an extremely competitive business, and if someone else’s page offers more features and dazzle than does yours, don't expect to hold an audience. It’s depressing to see your page’s hit counter stuck at 3 (especially when one of those visitors was your mother) for weeks on end!

There is a downside to that entire dazzle, however. Added functionality – whether it is animated graphics, sophisticated financial analysis capabilities, or whatever – requires both computer processing power and bandwidth, or the capability to transfer data between host and client systems. You can program all the fancy stuff you like on your host system, but if more than a few people log on at once, you’ll find the system slowing to a crawl and your net connection sorely overtaxed. This is a serious problem, because nothing turns potential users (and customers) away from a site more quickly than having to deal with sluggish response.

The answer is to let the client computer do some of the work – which is one of the rationales behind the development of JavaScript and VBScript. The instructions (script code) become part of the HTML file, and the client computer does the processing. With ActiveX and Java applets, this philosophy has been taken to an even higher level. Rather than downloading just a script, an entire program is downloaded to the client computer and then executed.

Let’s look at an example. Suppose a financial-service provider wants to let their customers visit their Web page, obtain historical stock prices for a specific issue, apply a variety of technical-analysis procedures to the data and display the results as graphs. Using the old methods, the analysis program would reside on the host computer. The processing would be done there, and the resulting graphs (and graphics files are usually pretty big) would be downloaded to the client for display. If the user wanted to make a slight change in the analysis parameters, the commands would have to be uploaded, the analysis repeated and the new graph downloaded again.

A better approach would be to download a copy of the analysis program, created either with Java or ActiveX, to the client computer. You might think that downloading a program would require more bandwidth than sending data back and forth, but that is often not the case. First of all both Java and ActiveX were designed specifically to create small, executable files, minimizing download time. Also, both techniques were designed to make use of intelligent caching, a system by which recently-used downloads are kept on the client system after being downloaded the first time. No download is required for subsequent uses.

While ActiveX and Java have similar goals, their method of implementation is different. Java is anew, object-oriented programming language that was developed at Sun Microsystems. ActiveX is the new name for Microsoft’s Object Linking and Embedding (OLE) controls. While different in many respects, the two standards do share similar features that optimize them for small components that are suitable for downloading. Both JavaScript and VBScript will eventually allow your Web pages to make use of Java applets and ActiveX controls.

VBScript and JavaScript have a great number of similarities. In fact, it’s probably accurate to say that they are more similar than they are different. After all, both are computer languages designed for essentially the same purpose. It is true that they have different histories. JavaScript is based on the C and C++ languages, while VBScript is a close cousin of interpreted Basic. Even so, both languages use '+' to mean addition, '=' for assignment, and '<' for less-than. Both have named variables, functions, loops, conditional statements, and so on. Once you know JavaScript, you'll find you can understand a lot of VBScript code, and vice versa.

Of course, if there weren't plenty of differences to trip you up, there would be far less need for articles that point them out. ;-)

 

On Program Comments

Like any standard programming language, VBScript allows for comments -- text in the code that is ignored by the script interpreter, but serves to document and explain the code's logic, meaning and operation. VBScript provides two ways to identify comments: The Rem statement and the apostrophe (').

The rule is mercifully simple. Any line that begins with Rem or an apostrophe is treated as a comment. You can also place a comment at the end of a line of code by preceding the comment portion with the standard Rem or apostrophe.

NOTE: When you use Rem, there must be colon (':') between the end of the code and the Rem comment.

Following are some examples of VBScript comments:

'This is a comment

Rem this is also a comment

Document write("Hello ") : Rem another comment

Document write("y’all") ' and yet another

I always advise readers to use comments liberally in their code, no matter which programming language they are using. You may think you will remember the details of the code you wrote – but unless your memory is a lot better than mine, you will be in for a nasty surprise when you look at the script several months down the road. Comments are also a great help when you let other programmers use your code, or when you examine some code you downloaded from the Web.

[Next IDM: data validation
in JavaScript and VBScript]

Buy the Book
Buy it Now

Paperback, 293 pages
(Coriolis Group)
September 1996
ISBN: 1883577977

The Author

Peter Aitken has written over 20 books on computers an software. He is a contributing editor to Visual Developer Magazine, where he writes "Basically Visual," a column for VB developers.

Adapted for IDM with permission of Coriolos Group.
Copyright © 1997 by Coriolos Group.

The Book

In Web Developer's Guide to JavaScript and VBScript (Coriolis Group, Sept 1996; ISBN 1883577977) author Peter Aitken takes readers on a tour of both major scripting languages. The introductory text will help non-programmers especially get started building more dynamic web pages.

Of Interest
· Intranet Exchange
Hear about and discuss Intranets with the people in the trenches.
 

Intranet News

Of Interest
· Intranet eXchange Discussion Board

· Advice and Opinions