VBScript and JScript Interactions Within ASP Pages

ASP is able to manage scripts written in different scripting languages, exploiting the right scripting engine in order to interpret statements and execute built-in functions. The ASP development environment comes with two scripting engines, VBScript (the default) and JScript. However, developers are not restricted to these two scripting languages. Virtually any scripting language can be used, as long as the ActiveX scripting engine is provided.

The choice of scripting language can originate from different reasons: it can be the language most familiar to the developer, offer the best features for a given project development, be the most efficient and so on. Regardless of the factors you take into account in choosing your scripting language, sometimes you may have to rewrite a function which is a built-in feature in another language. Or maybe you have already written the script you need, but using a different scripting language.

What can you do in such situations? Is it necessary to rewrite the script in the language you are now using? Or is it possible to use built-in functions and scripts written in other languages? In this article we will see how to make VBScript and JScript interact each other to get the best of both scripting languages' features in our ASP projects.

VBScript and JScript built-in functions

VBScript and JScript have a lot of built-in functions providing simple solutions to common programming problems. However not all built-in functions of a scripting language have corresponding functions in the other language. For instance, VBScript provides many functions for the manipulation of strings and the formatting of data, which don't exist as built-in functions in JScript. Examples of such functions are StrReverse() , Filter() , and FormatCurrency() . On the other hand, JScript provides built-in functions for the management of arrays, the coding and decoding of strings and the calculation of mathematical operations, which are not found in VBScript: join() , reverse() , pow() , bitwise operators, escape() , and unescape() .

What you can do if you need a VBScript function while writing a JScript script?

Calling VBScript functions from JScript and vice versa

If you want to use a VBScript built-in function from your JScript script, you have to write a VBScript user function calling the built-in one and call this user function from JScript script as a common JScript function.

For instance, if you want to use the VBScript FormatCurrency() built-in function, you have to define the following VBScript script:

<SCRIPT LANGUAGE="VBSCRIPT" RUNAT="SERVER">

Function FormatValue(Value)
     FormatValue = FormatCurrency(Value)
End Function

</SCRIPT>

Now you can use FormatValue() inside your JScript script as a normal JScript function. The same approach applies to VBScript scripts using JScript functions.

Following the same rule you can use any user-defined function from within any script. However you must be careful when calling a VBScript procedure ( Sub ) without arguments from a JScript script. In this case you must use the JScript calling convention as though it were a JScript function with no arguments. For example you would use foo() to call a VBScript Sub foo procedure.

Sharing data

Although mixing VBScript and JScript functions could be very helpful in some situations, sharing data among scripts in different languages can be useful, too. The way to do this is very simple: you can just use any variable that has a page level scope regardless the language used to declare it.

You can use objects in the same way, reading or modifying properties or calling methods in the usual way, using your favorite language. Of course properties and methods of a given object are those defined by the language you have used to instantiate it. As in the case of VBScript procedure calling, when you call a method without arguments of a VBScript object from a JScript script you must follow the JScript calling convention and vice versa.

Managing arrays

Shared arrays need some special consideration. Arrays can be shared across scripts written using different languages, as with any other variable. However, some compatibility notes have to be taken into account.

A VBScript array can be used in the JScript environment using the indexing notation of VBScript. So you would reference an array element using the syntax myArray(2) and not the JScript notation myArray[2] .

You can also convert a VBScript array into a JScript array using a special JScript object: the VBArray object. The following JScript code creates the JScript array myJSArray from a VBScript array myVBArray :

var Temp = new VBArray(myVBArray)
var myJSArray
myJSArray = Temp.toArray()

This code creates a temporary VBArray object first, and then uses its method toArray() to convert it into a JScript array. The array myJSArray can be used as a regular JScript array, using the syntax myJSArray[1] . However the method toArray() converts a multidimensional VBArray into a single dimensional JScript array. Each successive dimension is appended to the end of the previous one.

If you want to use JScript arrays from VBScript you will face some difficulties. Indeed, although you can access JScript array-specific methods and properties, there is currently no way to access the single elements of a JScript array. So, you can read the specific length property of a JScript array from a VBScript script, as in the following example:

x = myJSArray.length

However, you have no access to an element of this array, and the following VBScript code will not work:

x = myJSArray(3)

A possible solution to this problem involves an elaborate conversion sequence, as showed in the following script, where VBScript is assumed as the default scripting language:

<%

Dim Temp
Dim myVBArray
Temp = myJSArray.join(", ")
myVBArray = Split(Temp, ", ")

%>

Here the JScript join() method converts the array myJSArray elements into a comma delimited string. Next the VBScript Split() function converts the string back into a VBScript array. Notice that the JScript join() method has been called from the VBScript environment. Following this example, you can simulate the toArray() method of the JScript VBArray object by defining a VBScript function to convert JScript arrays into VBScript ones.

Conclusions

Using different scripting languages in one ASP project offers many benefits during development, but making these languages interact with each other gives the developer the great opportunity of having a powerful development platform collecting built-in functions and other capabilities from different scripting languages. This makes possible the implementation of script libraries that can be used from both VBScript and JScript environments.