VBScript and JavaScript mutual call method
The ASP has the ability to manage different language scripts, which can automatically call the appropriate scripting engine to explain script code and perform built-in functions. The ASP development environment provides two scripting engines, namely VBScript, and JScript. However, developers are not limited to only two languages, as long as they can provide the appropriate ActiveX scripting engine to use any scripting languages.
The selection of scripting languages is often based on many different reasons: it may be the most familiar language of developers, possibly to provide maximum feature support for a given project, or may be the most efficient. Different environments and requirements make us pay attention to different factors when choosing scripting languages, and also makes us not directly provide other language inherent functions, or a script is already written. It is another scripting language.
What should I do at this time? Do you need to rewrite these scripts with the scripting language used? Or is it possible to call other scripting's built-in functions in a scripting language? This article wants to make the VBScript script and JScript script interact in the ASP application to maximize the feature support for both scripting languages.
First, the built-in function of VBScript and JScript
In VBScript and JScript, a large number of built-in function functions are the same or similar. However, functions built in a scripting language are not always corresponding in another scripting language. For example, VBScript provides a number of functions for operating strings and formatted data, which do not exist in JScript. These functions include strreverse (), filter (), and formatcurrency (). On the other hand, functions provided by JScript for managing arrays, string coding, etc. are not defined in VBScript, such as Join (), Reverse (), POW (), bit operation, escape (), and unescape () Wait.
So, what should I do if I need a VBScript function in a JScript program?
Second, the mutual call of the heterogeneous script
If you need to call a function in a VBScript in the JScript script, you should write a VBScript user-defined function (call the VBScript built-in function here) and then call the Used JScript function in the JScript script call this user-defined function.
For example, if the VBScript built-in function to be called is formatcurrency (), you can declare the following custom functions:
Function FormatValue (Value)
FormatValue = formatcurrency (Value)
END FUNCTION
Script>
Next, you can call formatvalue () like a normal JScript function in the JScript code. Use a similar approach to the VBScript code to call the JScript function.
Applying the same rule, we can call any user-defined functions within any script. However, when calling a VBScript process (SUB) in JScript scripts (SUB) should be omitted, at which point in JScript should call it like a JScript function that invokes a non-parameter, if you use foo () call VBScript SUB FOO process.
Third, data sharing
It is very useful to mix the VBScript and JScript functions in some cases, but share data between different language scripts may also be useful. The method of implementing this sharing is simple: no matter what language is used, as long as it is a variable in the page-level declaration, it can be arbitrarily referenced.
The method of use of the object is also similar, and the method of reading, modifying attributes, or calling an object can be arbitrarily selected. Of course, the properties and methods of a given object are defined by the language that creates the object instance. As the process call of the above VBScript, when the method of calling a VBScript object without parameters is called from the JScript, its calling method also follows the Call rules of JScript, and vice versa. Fourth, array management
Array sharing problems are slightly complicated. Although arrays can be shared between different language scripts like other variables, they must pay attention to compatibility issues.
The VBScript array can be referenced by VBScript in JScript, i.e., using MyArray (2) references array elements instead of JScript array element reference symbol MyArray [2]. In addition, you can use a special JScript object-VBarray object to convert the VBScript array to the JScript array. The following code creates a JScript array 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 yourself to the JScript array. Thereafter, you can use MyjsArray like a normal JScript array such as MyjsArray [1]. However, it should be noted that the toarray () method will convert a multi-dimensional VBARRAY to a one-dimensional JScript array.
Quote the JScript array from VBScript is more complicated. Although we can directly access the JScript array related methods and properties directly in VBScript, there is no way to access a single element of the JScript array. That is, we can read the length properties of the JScript array in the VBScript script, as shown below:
x = myjsarray.length
But you can't read the individual elements of the array directly, the following VBScript code is incorrect:
x = myjsarray (3)
One possible way to solve this problem is to perform a conversion process, as shown below, assuming that VBScript is the default scripting language:
<%
DIM TEMP
DIM Myvbarray
Temp = myjsarray.join (",")
Myvbarray = split (temp, ",")
%>
The JScript Join () method here converts the array myjsArray element to a string with commas divided, and the VBScript split () function converts strings to VBScript arrays. Note that we are a JScript JoIn method that is called JScript in the VBScript environment. According to this example, we can simulate the TOARRAY () method of the VBARRAY object of JScript to implement the conversion of the JScript array to the VBScript array.
Five, small knot
Flexible in the same ASP engineering has many advantages, with many of these scripts, more interactive capabilities, more functions provided by developers, and other functions, and other functions, and make it both A general script library that can be used for VBScript can also be used for JScript environments.