
| 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 functionsVBScript 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 What you can do if you need a VBScript function while writing a JScript script? Calling VBScript functions from JScript and vice versaIf 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 <SCRIPT LANGUAGE="VBSCRIPT" RUNAT="SERVER"> Function FormatValue(Value) </SCRIPT> Now you can use 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 ( Sharing dataAlthough 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 arraysShared 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 You can also convert a VBScript array into a JScript array using a
special JScript object: the
This code creates a temporary 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
However, you have no access to an element of this array, and the following VBScript code will not work:
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:
Here the JScript ConclusionsUsing 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. |