VBScript and JScript (cactus studio · CPCW) in ASP page
ASP has the ability to manage different language script programs. It can automatically call the appropriate script engine to explain the script code and execute the built -in function. The ASP development environment provides two scripts engines, namely VBSCRIPT and JScript. However, developers are not limited to only two languages, as long as they can provide a suitable ActiveX script engine, they can use any script language.
The choice of script language is often based on many different reasons: it may be the most familiar language of developers, it may provide the most characteristic support for a given project, or it may be the most efficient. Different environments and requirements make us pay attention to different factors when choosing a script language. At the same time, it also makes us facing the problem that the script language we faces at some point cannot directly provide other languages inherently. It uses another script language.
What should I do at this time? Do I need to rewrite these scripts with the current script language? In other words, is it possible to call the built -in function of other script language in a script language? This article explains how to allow the VBScript script and JScript script to interact in ASP applications to maximize the characteristics of the two script language.
1. The built -in functions of VBScript and JScript
In VBScript and JScript, a large number of built -in function functions are the same or similar. However, the built -in function in a script language is not always a corresponding function in another script language. For example, VBScript provides many functions for operating string and formatting data, which do not exist in JScript. These functions include Strreverse (), Filter (), and Formatcurrency (). On the other hand, the functions provided by JSCRIPT for managing array, string coding, etc. are not defined in VBScript, such as Join (), Reverse (), POW (), bit operations, eScape (), and UNESCAPE () wait.
So, what if you need a VBScript function in the JScript program?
2. Mutual calls of heterogeneous scripts
If you need to call a built -in function in a VBScript script in the JScript script, you should write a VBScript user definition function (where to call the VBScript built -in function here), and then call this user definition function like the public JScript function in the JScript script.
For example, if the VBScript built -in function 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>
Next, you can call FormatValue () like an ordinary JScript function in the JScript code. Similar methods can also be used to call the jscript function of VBScript code.
Apply the same rule, we can call any user definition function in any script. However, when calling a VBScript process (SUB) without parameters (SUB) from the JScript script. At this time, you should call it like a JScript function without parameters in JScript, calling it with FOO () to call the VBScript Sub FOO process.
Third, data sharing
Under certain circumstances, mixing the use of VBSCRIPT and JSCRIPT functions is very useful, but sharing data between different language scripts may also be useful. The method of implementing this sharing is very simple: no matter what language is used, as long as the variables on the page -level statement can be referenced arbitrarily.
The method of the object is also similar, and you can use the appropriate language reading, modify the method of the attribute or call the object. Of course, the attributes and methods of the given object are defined by the language of creating an instance of the object. Just as the process calls of the VBScript process above, when a method of calling a VBScript object with a parameter with a parameter is called from the JScript, the call method also follows the call rules of JScript, and vice versa.
Fourth, array management
The array sharing problem is slightly more complicated. Although the array can be shared between different language scripts like other variables, we must pay attention to compatible issues.
The VBScript array can be referenced with a symbol of VBScript under JScript, that is, the array element of the array elements instead of JScript's array elements reference symbol MyARray [2] In addition, you can use a special JSCRIPT object -the VBARRAY object to convert the VBScript array to the JScript array. The following code creates the JScript array Myjsarray from the VBScript array Myvbarray:
var test = new vbarray (myvbarray)
var myjsarray
myjsarray = temp.toarray ()
The above code first creates a temporary Vbarray object, and then use its toarray () method to convert itself to the JScript array. Since then, I can use myjsarray like an ordinary JScript array, such as Myjsarray [1]. However, it should be noted that the toarray () method will convert a multi -dimensional VBARAY into one -dimensional JScript array.
The JScript array is more complicated from VBScript. Although we can directly access the methods and attributes of the JScript array in VBScript, there is no method to directly access the 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, it is impossible to read the single element of the array directly. The VBScript code below is incorrect:
x = myjsarray (3)
A feasible method to solve this problem is to perform a conversion process. As shown in the following code, it is assumed that VBScript is a default script 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 a comma as a division, and the VBScript Split () function converts the string to the VBScript array. Note that we here are the Join method of calling JScript under the VBScript environment. According to this example, we can simulate the toarray () method of the VBARRAY object of JScript by custom VBSCRIPT function to implement the conversion from the JScript array to the VBScript array.
Five, Summary
The flexible selection of different scripting languages in the same ASP project has many advantages. The interaction ability between these scripts is more interactive to integrate the built -in functions provided by different languages and other functions. It is possible to be used for VBSCRIPT or universal script libraries that can be used in the JScript environment.