Implementing VBScript and JavaScript calling methods

Source: Internet
Author: User
Tags array arrays functions implement join split
Javascript|vbscript ASP has the ability to manage scripting programs in different languages and can automatically invoke the appropriate scripting engine to interpret script code and execute built-in functions. The ASP development environment provides two scripting engines, VBScript (default) and JScript. However, developers are not limited to using both languages, and can use any scripting language if they can provide the right ActiveX scripting engine.
Scripting languages are often chosen for a number of different reasons: it may be the most familiar language for developers, probably the most characteristic support for a given project, and possibly the most efficient. Different environments and requirements allow us to focus on different factors when choosing scripting languages, and at some point we face the problem that the selected scripting language does not directly provide functions that are intrinsic to other languages, or that a script has been written but used in another scripting language.
What should I do now? Do you need to rewrite these scripts with the scripting language you are currently using? Or is it possible to invoke a built-in function of another scripting language in a scripting language? This article is about how to get VBScript scripts and JScript scripts to interact with each other in ASP applications to maximize the feature support for both scripting languages.
One, VBScript, and JScript built-in functions
In VBScript and JScript, there are a number of built-in function functions that are the same or similar. However, functions built into a scripting language do not always have corresponding functions in another scripting language. For example, VBScript provides a number of functions for manipulating strings and formatting data that do not exist in JScript. These functions include StrReverse (), Filter (), and FormatCurrency (). On the other hand, the functions provided by JScript for managing arrays, string encodings, and so on are not defined in VBScript, such as join (), reverse (), POW (), bitwise manipulation, Escape (), and unescape ().
So what if you need a VBScript function in a JScript program?
Two, different kinds of scripts to call each other
If you need to call a built-in function in VBScript in a JScript script, you should write a VBScript user-defined function (where the VBScript built-in function is called), This user-defined function is then called in a JScript script as if it were a public JScript function.

For example, if the VBScript built-in function to invoke is FormatCurrency (), you can declare the following custom function:

< SCRIPT language= "VBSCRIPT" runat= "SERVER" >
Function Formatvalue (Value)
Formatvalue = FormatCurrency (Value)
End Function
</script>

You can then call Formatvalue () like a normal JScript function in JScript code. You can also implement VBScript code to call a JScript function in a similar way.

Applying the same rules, we can invoke any user-defined function within any script. However, when you call a VBScript procedure (SUB) without parameters from within a JScript script, you should note that you should call it in JScript as if you were calling a JScript function without parameters, such as calling the VBScript Sub foo process with foo ().

Third, data sharing

It is useful to mix VBScript and JScript functions in some cases, but it can also be useful to share data between scripts in different languages. The way to implement this sharing is simple: regardless of the language you are using, you can refer to any variable that is declared at the page level.

Objects are similarly used, and you can choose the appropriate language to read, modify, or invoke the method of the object. Of course, the properties and methods of a given object are defined by the language that created the object instance. As with the procedure call in VBScript above, when you call a method of a VBScript object without parameters from JScript, the calling method also complies with the calling rules of JScript and vice versa.

Iv. Array Management

The problem of array sharing is a little more complicated. Although arrays can be shared among different language scripts like other variables, you must be aware of compatibility issues.

The VBScript array can be referenced in VBScript under JScript, that is, myarray (2) refers to an array element instead of a JScript array element reference 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 code above first creates a temporary VBArray object and then uses its ToArray () method to convert itself to a JScript array. You can then use myjsarray like a normal JScript array, such as myjsarray[1]. It should be noted, however, that the ToArray () method converts a multidimensional vbarray into a one-dimensional, JScript array.

Referencing a JScript array from VBScript is more complex. Although in VBScript we have direct access to the methods and properties associated with the JScript array, there is no way to directly access the individual elements of the JScript array. That is, we can read the length properties of the JScript array in the VBScript script as follows:

x = Myjsarray.length

But the single element of the array cannot be read directly, and the following VBScript code is incorrect:

x = Myjsarray (3)

One possible way to resolve this problem is to perform a conversion process, as shown in the following code, where VBScript is assumed to be 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 comma-delimited string, and the VBScript split () function converts the string to a VBScript array. Notice here we are calling the join method of JScript in a VBScript environment. In this example, we can simulate the ToArray () method of JScript's VBArray object by customizing the VBScript function to implement the conversion of a JScript array to a VBScript array.

Summary
The flexibility to use different scripting languages within the same ASP project has many advantages, and the interactive capabilities of these scripts provide more opportunities for developers to integrate the built-in functions and other features provided by different languages. It also makes it possible to implement a Generic script library that can be used both for VBScript and for the JScript environment.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.