DHTML Lab: JavaScript Enhancement with VBScript | 4 | WebReference

DHTML Lab: JavaScript Enhancement with VBScript | 4


JavaScript Enhancement with VBScript
VBScript functions

The getHex() Function

<SCRIPT LANGUAGE="VBScript" TYPE="text/vbscript">
   Function getHex(num)
      getHex = Hex(num)
   end Function

The function above simply takes one argument and calls the build-in Hex() function to return the hexadecimal equivalent of the argument.

Notice these differences from a JavaScript function:

  1. VBScript is not case-sensitive. The Function keyword has an initial cap to conform with accepted VBScript conventions. It could also be spelled "function" or "FuNcTiOn".
  2. The function statements are not enclosed in braces ({}). They are simply placed one-on-a-line following the function identifier.
  3. Statements do not end with a semi-colon(;). Every line is considered a separate statement.
  4. The function ends with the end Function statement, instead of a closing brace.
  5. A return value for the function is defined by assigning the value-to-be-returned to the function name! Therefore, getHex = Hex(num) is equivalent to the JavaScript return Hex(num), assuming, of course, that a Hex() JS function existed.

Calling a VBScript Function from JavaScript

The getHex() function, defined above, is available to both VBScript and JavaScript. No extra statements or identifiers are necessary.

The following form illustrates this:
The HTML for the form is:

This only works in Explorer, of course. But you can appreciate how a browser-specific intranet might benefit from a series of such functions that in-turn call built-in VBScript functions, speeding up statement processing and reducing script size.


The following button works for both browsers and calls a JavaScript function. If Explorer is being used, the JS function calls a VBScript function, otherwise the while loop is executed:

The required HTML is:

<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
   function getLength(str){
      if (IE4) str = vbRTrim(str);
      else {
         while (str.substr(str.length-1)==" ") {
            str = str.substring(0,str.length-1);
      return str.length
<SCRIPT LANGUAGE="VBScript" TYPE="text/vbscript">
   Function vbRTrim(str)
      vbRTrim = RTrim(str)
   end Function
<INPUT TYPE=TEXT VALUE="Length should be 19               " SIZE=40>

Useless, You Say?

Perhaps the examples on this page have not convinced you to enhance Explorer with JavaScript-VBScript communication. They have limited functionality and were only created to introduce VBScript functions and the principles of communication. Hopefully that has been achieved.

On the remaining pages, we will create a larger and more convincing application.

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-vbFunct.html