DHTML Lab: JavaScript Enhancement with VBScript | 6 | WebReference

DHTML Lab: JavaScript Enhancement with VBScript | 6


JavaScript Enhancement with VBScript
why communicate?

VBScript, a.k.a. Visual Basic Scripting Edition, remains mostly unused by Web authors because of its major weaknesses:

  1. It is supported only by Internet Explorer.
  2. It is supported only on the Windows OS.
  3. It appeals only to seasoned Visual Basic programmers, and not the neophyte HTML-oriented scripter.

Then, why bother with it?

Regular readers of this column know that the most common used statement in cross-browser DHTML scripting is: "if...else." Cross-browser scripting with JavaScript is a reality, but only as far as the core JavaScript language is concerned. Both browser implementations of the core language are similar. JScript 3.0 is almost identical to JavaScript 1.2/1.3. However, those of us who rely heavily on arrays in our scripts are frustrated by JScript's limited array methods. And have you ever tried to prototype the event object in Explorer?

The different Document Object Models of the browsers make the core language similarities almost irrelevant, since we must branch to different statements to perform the same tasks for Navigator and Explorer.

Therefore, the above question can be reformulated as: Why not bother with it?

We execute conditional statements anyway, so why not add additional fuctionality to our scripts, or our Web pages, if we can? Provided, of course, that we do it simply, and cleanly, without harming the performance of non-conforming browsers?

Also, most intranets are browser-specific, so VBScript may be a way to enhance performance in Explorer-only environments.

Is It Really That Good?

No, but...

It has several features that may save you the headache of creating JavaScript workarounds, or simply add Explorer-specific functionality.

For example:

String Functions

In the following example, the INPUT box contains a string with trailing spaces. How do we get the true length of the string? We very often need to do this when processing user form input:

Click the first button. The alert box gives us the incorrect length.
The second button calls a cross-browser JavaScript function that loops through the string removing one trailing space at a time.
The third button (IE-only) calls built-in VBScript functions resulting in the correct answer with much less code. The script/HTML for the above looks like this:
<SCRIPT LANGUAGE=JavaScript TYPE="text/javascript">
   function getLength(str){
      while (str.substr(str.length-1)==" ") {
         str = str.substring(0,str.length-1);
      return str.length
   VALUE="Length should be 19               " SIZE=40><BR>
   onClick="alert(Len(Rtrim(Example1.value)))">VB Length</BUTTON>

The RTrim() function takes a string argument and returns the string with the trailing spaces removed. The Len() function is, of course, similar to the JS length() method. Other useful VBScript string functions include:

Math Functions

The following text box contains a decimal integer to be converted to a hexadecimal value:
Both buttons give us the correct value. Compare the script that's necessary, however:
<SCRIPT LANGUAGE=JavaScript TYPE="text/javascript">
function getHex(num){
   hexStr = "0123456789ABCDEF";
   if (num>=16) {
      hex = hexStr.substr(parseInt(num/16),1);
      num = num%16;
   hex += hexStr.substr(num,1);
   return hex;
   onClick="alert(Hex(Example2.value))">VB Hex</BUTTON>

The built-in Hex() function returns a hexadecimal value, whereas we had to create a clumsy function to do the same in JavaScript.

There are many other useful features in VBScript, including its event handling standard and unique Date constants.

It is not the purpose of this article to discuss all of these, nor will we discuss in any detail the structure of the VBScript language. Our intention is to introduce the concept of JavaScript-VBScript communication, that is, using VBScript features from JavaScript, and develop an application to illustrate it. No knowledge of VBScript is required and the short VBScript code produced can be easily cut and pasted.

So, first let's look at how communication is established.

Produced by Peter Belesis and

All Rights Reserved. Legal Notices.
Created: Nov. 18, 1998
Revised: Nov. 18, 1998

URL: http://www.webreference.com/dhtml/column22/js-vbIntro.html