[Excellent] Methods for mutual call between VBScript and JavaScript

Source: Internet
Author: User
Methods for mutual call between VBScript and JavaScript ASP provides the ability to manage script programs in different languages. It can automatically call appropriate script engines to explain script code and execute built-in functions. The ASP development environment provides two script engines: VBScript (default) and JScript. However, developers are not limited to using only these two languages, as long as they can provide the appropriate ActiveX Scripting Engine to use any scripting language.

Script language selection is often based on many different reasons: it may be the most familiar language for developers, it may be the most distinctive support for a given project, it may also be the most efficient. Different Environments and requirements enable us to focus on different factors when selecting a scripting language, at the same time, it also makes it difficult for us to directly provide functions inherent in other languages for the selected scripting language, or a script has been written but used in another scripting language.

What should I do now? Do you need to rewrite these scripts in the current scripting language? Or is it possible to call the built-in functions of other scripting languages in one scripting language? This article describes how to allow VBScript to interact with JScript to maximize the special support of the two scripting languages in ASP applications.

I. built-in functions of VBScript and JScript

In VBScript and JScript, a large number of built-in functions are the same or similar. However, the built-in functions in one scripting language do not always have corresponding functions in another scripting language. For example, VBScript provides many functions for operating strings and formatting data. These functions do not exist in JScript. These functions include strreverse (), filter (), and formatcurrency. On the other hand, functions provided by jscript for Array management and string encoding are not defined in VBScript, such as join (), reverse (), and pow (), bit operations, escape (), and Unescape.

So what if a VBScript function is required in a JScript program?

Ii. 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.

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.