JavaScript (JScript) and VBScript data sharing and object sharing.

Source: Internet
Author: User
Tags array array length object end variables split variable access
Javascript|js|jscript|vbscript| Object | data

If there are any flaws, please point out, thank you.

1. General Global Variables
In browser, there are many ways to refer to a global variable, you can refer to the variable directly, You can also use window.variable to refer to global variables because global variables belong to the window's form, but when each new window opens, the browser automatically empties the previously existing variables (that is, each new page is a "Clean Web page"), Therefore, in the newly opened window is not to refer to the previously defined variables, if you want to refer to, can be changed to get, such as through the parameters of the URL to pass the method to obtain, but this is something:
<script>
var global = "This is a global var."
</script>
<script language= "VbScript" >
<!--
Alert (Window.global)
Alert (Global)
-->
</SCRIPT>

Output results:
This is a global var.
This is a global var.

2, the VBS refers to JS object (non-prototype prototype object), function
Can be directly referenced: that is, the object name. Property name

<script language= "JavaScript" >
<!--
function obj () {};
obj.string= "Test";
Obj.replacec=function (s) {return s.replace (/c/, "")};
-->
</SCRIPT>
<script language= "VbScript" >
<!--
Alert (obj.string)
obj.string= "Change"
Alert (obj.string)
Alert (OBJ.REPLACEC (obj.string))
-->
</SCRIPT>

Output results:
Change
Hange

When JS is a prototype type, VBS can use the following methods to access the members of JS

<script language= "JavaScript" >
<!--
function MyClass (s)
{
This.s=s
return this;
};
Myclass.prototype.output=function ()
{
alert (THIS.S);
}
var obj=new myClass ("Bluedestiny");
-->
</SCRIPT>
<script language= "VbScript" >
<!--
Obj.output ()
-->
</SCRIPT>

3, JS call in the VBS class, you can first in the VBS as an example, JS can be accessed through this instance of the members of the VBS.

<script language= "VbScript" >
<!--
Class MyClass
Public s
Public Sub Alertstring ()
Alert (s)
End Sub
End Class
Set Obj=new MyClass
-->
</SCRIPT>
<script language= "JavaScript" >
<!--
Obj.s= "Bluedestiny"
Obj.alertstring ()
-->
</SCRIPT>
Output results:
Bluedestiny

4, JS call to the definition of VBS array problem, you can directly use subscript to get the value of the first few elements.

<script language= "VbScript" >
Dim A (2)
A (0) = "Blue"
A (1) = "Destiny"
</script>

<script>
<!--
Alert (A (0))
Alert (A (1))
-->
</SCRIPT>
Output results:
Blue
Destiny

But how do you convert an array of VBS to an array of JS? Using the VBArray () object of JS and the ToArray () method, it is noteworthy that the ToArray () method obtains a one-dimensional array:

<script language= "VBScript" >
<!--
Function Createvbarray ()
Dim I, J, K
Dim A (2, 2)
' Initialize a two-dimensional array of
K = 1
For i = 0 to 2
For j = 0 to 2
A (j, i) = k
Document.writeln (k)
K = k + 1
Next
Document.writeln ("<BR>")
Next
Createvbarray = A
End Function
-->
</SCRIPT>

<script language= "JScript" >
<!--
function Vbarraytest (VBArray)
{
Call the VBArray () object, convert the VBS array to a JS array, and invoke the ToArray () method to get the one-dimensional array.
var a = new VBArray (VBArray);
var B = A.toarray ();
var i;
for (i = 0; i < 9; i++)
{
Document.writeln (B[i]);
}

}
Vbarraytest (Createvbarray ());
-->
</SCRIPT>
Output results:
1 2 3
4 5 6
7 8 9
1 2 3 4 5 6 7 8 9

5, JS array into the VBS array:
To get the array length of JS, it can be obtained directly with the length property

<script language= "JavaScript" >
<!--
var arr=["Blue", "Destiny"];
-->
</SCRIPT>
<script language= "VbScript" >
<!--
Alert (arr.length)
-->
</SCRIPT>

Output results:
2

If you want to access the array elements of JS through the subscript, you will not be able to get it, but you can do so by using the existing method: Join () (method in JS) and split ()

<script language= "VbScript" >
<!--
' Note: The Join method for the VBS is not available here (arr, ","), and is actually used here in JS.
Vbarr=arr.join (",")
Vbarr=split (Vbarr, ",")
Alert (Vbarr (0))
Alert (Vbarr (1))
-->
</SCRIPT>

Output results
Blue
Destiny



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.