Reference
Mutual calls of heterogeneous scripts
If you need to call a built-in function in VBScript in a JScript script, you should write a VBScript User-Defined Function (call the built-in function in VBScript here ), then, the user-defined function is called in the JScript script just like the common JScript function.
For example, if the built-in function of VBScript to be called is formatcurrency (), you can declare the following custom function:
<Script language = "VBScript" runat = "server">
Function formatvalue (value)
Formatvalue = formatcurrency (value)
End Function
</SCRIPT>
In JScript code, you can call formatvalue () like a common JScript function. Similar methods can also be used to implement VBScript code to call the JScript function.
With the same rules, we can call any user-defined function in any script. However, you should pay attention to calling a VBScript process (sub) without parameters from a JScript script. In JScript, you should call it like calling a JScript function without parameters, for example, use Foo () to call the VBScript sub Foo process.
Iii. Data Sharing
In some cases, mixed use of VBScript and JScript functions is very useful, but sharing data between scripts in different languages may also be very useful. The sharing method is simple: No matter what language is used, variables declared at the page level can be referenced at will.
The usage of an object is similar. You can select an appropriate language to read, modify attributes, or call an object method. Of course, the attributes and methods of the given object are defined by the language in which the object instance is created. As in the preceding example, when a VBScript object without parameters is called from JScript, the calling method also complies with the calling rules of JScript, and vice versa.
Iv. array Management
Array sharing is a little more complicated. Although Arrays can be shared between scripts in different languages like other variables, you must pay attention to compatibility issues.
VBScript Arrays can be referenced by VBScript symbols in JScript, that is, using myarray (2) to reference array elements rather than the array elements of JScript to reference the symbol myarray [2]. In addition, you can use a special JScript object-vbarray object to convert a VBScript array to a JScript array. The following code creates a JScript array myjsarray from the VBScript array myvbarray:
VaR temp = new vbarray (myvbarray)
VaR myjsarray
Myjsarray = temp. toarray ()
The above code first creates a temporary vbarray object, and then uses its toarray () method to convert itself into a JScript array. Then, you can use myjsarray like a common JScript array, for example, myjsarray [1]. However, the toarray () method converts a multi-dimensional vbarray into a one-dimensional JScript array.
It is more complicated to reference the JScript array from VBScript. Although VBScript can directly access methods and attributes related to the JScript array, it does not directly access a single element of the JScript array. In other words, we can read the Length attribute of the JScript array in the VBSCRIPT script, as shown below:
X = myjsarray. Length
However, you cannot directly read a single element of the array. The following VBScript code is incorrect:
X = myjsarray (3)
A feasible solution to this problem is to execute a conversion process, as shown in the following code. It is assumed that VBScript is the default script language:
<%
Dim temp
Dim myvbarray
Temp = myjsarray. Join (",")
Myvbarray = Split (temp ,",")
%>
The jscript join () method converts the array myjsarray element to a comma-separated string. The VBScript split () function converts the string to a VBScript array. Note that the join method of JScript is called in the VBScript environment. In this example, we can use the custom VBScript function to simulate the toarray () method of the vbarray object of JScript to convert the JScript array to the VBScript array.
V. Summary
Flexible selection of different scripting languages in the same ASP project has many advantages. The interaction between these scripts brings more opportunities for developers to integrate built-in functions and other functions provided by different languages, it also makes it possible to implement a Universal Script library that can be used in both VBScript and JScript environments.