Converting VBScript Array to Javascript Array in the same ASP Page (1D and 2D) by Anand Venkatraman

Converting VBScript Array to Javascript Array in the same ASP Page (1D and 2D) by Anand Venkatraman We come across lot of times a need to convert an Array from ASP page to Javscript, so that we can manipulate in the HTML form using Javascript. Here is an example of how to do that. I have two include files, one for 1D array, one for 2D ar ...

Contributed by
Rating: 4 stars4 stars4 stars4 stars4 stars / 24
January 01, 2003
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

 

We come across lot of times a need to convert an Array from ASP page to Javscript, so that we can manipulate in the HTML form using Javascript. Here is an example of how to do that. I have two include files, one for 1D array, one for 2D array and one example page calling those files. You can modify the array, ie calling from a Database (result set) or a COM Component (VB Array) and have the array in Javascript and populate HTML forms and manipulate. Some documentation is available in the code itself. Happy programming!!!

Any questions regarding these files, please feel free to contact anand_venkatraman@yahoo.com

 

This is the calling page code

<!-- #INCLUDE FILE = "VB2JSarray2D.inc" -->
<!-- #INCLUDE FILE = "VB2JSarray1D.inc" -->
<HTML>
<HEAD>
<!--What ever U wanna write -->
</HEAD>
<BODY>
<!--What ever U wanna write -->
<%
Dim arrUserInfo1D(4)

arrUserInfo1D(0) = "Name1"
arrUserInfo1D(1) = "Name2"
arrUserInfo1D(2) = "Name3"
arrUserInfo1D(3) = "Name4"
arrUserInfo1D(4) = "Name5"

Call ConvertToJSArray1D(arrUserInfo1D,"arrUserInfo1D")
Response.Write("<H2>One Dimensional Array</H2>")
%>

<SCRIPT LANGUAGE="JAVASCRIPT">

document.write("<table border=1>");
for (i=0; i < arrUserInfo1D.length; i++)
{
    document.write("<tr>");
document.write("<td>");
    document.write(arrUserInfo1D[i]);
    document.write("</td>");
    document.write("</tr>");
}
document.write("</table>");

</SCRIPT>

<%

Dim arrUserInfo2D(2,4)

arrUserInfo2D(0,0) = "FirstName1"
arrUserInfo2D(0,1) = "FirstName2"
arrUserInfo2D(0,2) = "FirstName3"
arrUserInfo2D(0,3) = "FirstName4"
arrUserInfo2D(0,4) = "FirstName5"

arrUserInfo2D(1,0) = "LastName1"
arrUserInfo2D(1,1) = "LastName2"
arrUserInfo2D(1,2) = "LastName3"
arrUserInfo2D(1,3) = "LastName4"
arrUserInfo2D(1,4) = "LastName5"

arrUserInfo2D(2,0) = "Age1"
arrUserInfo2D(2,1) = "Age2"
arrUserInfo2D(2,2) = "Age3"
arrUserInfo2D(2,3) = "Age4"
arrUserInfo2D(2,4) = "Age5"

Call ConvertToJSArray2D(arrUserInfo2D,"arrUserInfo2D")
Response.Write("<H2>Two Dimensional Array</H2>")

%>

<SCRIPT LANGUAGE="JAVASCRIPT">

document.write("<TABLE BORDER='1'>");
for (i=0; i < arrUserInfo2D.length; i++)
{
    document.write("<TR>");
    for (j=0; j < arrUserInfo2D[i].length; j++)
    {
    document.write("<TD>");
        document.write(arrUserInfo2D[i][j]);
        document.write("</TD>");
    }
        document.write("</TR>");
}
document.write("</TABLE>");

</SCRIPT>
<!--What ever U wanna write -->
</BODY>
</HTML>

1st Include File

<%
'**************************************************
'Name        : VB2JSarray1D.inc
'Description    : Include File used to convert a VBScript One Dimensional Array to One Dimensional Javascript Array
'Developer    : Anand Venkatraman
'Creation Date    : 03/31/2000
'**************************************************

'This function converts a VBScript Array to Javascript Array(One Dimensional array only)
'Inputs are VBScript array and Javascript array name
'For the sake of variable names "mismatch", "vb2js" is prefixed to all the variables
'For eg. in ASP page you might call this function as "Call ConvertToJSArray(arrUserList,"arrUserList")
'When this function is called immediately Javascript array is created and written to the web page
'So we cannot use Response.Redirect after calling this function. You can View the Source of HTML page
'to see the Javascript array.

Function ConvertToJSArray1D(VBArray , ArrayName)

Dim vb2jsRow , vb2jsStr, vb2jsi
vb2jsRow = Ubound(VBArray,1)
%>
<SCRIPT LANGUAGE = 'JAVASCRIPT' >
var vb2jsi
<%=ArrayName%> = new Array(<%=vb2jsRow+1%>);
for (vb2jsi=0; vb2jsi < <%=vb2jsRow+1%>; vb2jsi++)
{
    <%=ArrayName%>[vb2jsi]= " "
}
</SCRIPT>
<%
Response.Write("<SCR"&"IPT LANGUAGE = 'JAVASCRIPT' >"&chr(13))
for vb2jsi=0 to vb2jsRow
    vb2jsstr = "VBArray("&vb2jsi&")"
%>    
    <%=ArrayName%>[<%=vb2jsi%>]= "<%=trim(eval(vb2jsstr))%>"
<%
Next
Response.Write("</SCR"&"IPT>")
End Function
%>

Second Include File

<%
'***************************************************
'Name        : VB2JSarray2D.inc
'Description    : Include File used to convert a VBScript Two Dimensional Array to Two Dimensional Javascript Array
'Developer    : Anand Venkatraman
'Creation Date    : 03/31/2000
'***************************************************

'This function converts a VBScript Array to Javascript Array(Two Dimensional array only)
'Inputs are VBScript array and Javascript array name
'For the sake of variable names "mismatch", "vb2js" is prefixed to all the variables
'For eg. in ASP page you might call this function as "Call ConvertToJSArray(arrUserList,"arrUserList")
'When this function is called immediately Javascript array is created and written to the web page
'So we cannot use Response.Redirect after calling this function. You can View the Source of HTML page
'to see the Javascript array.

Function ConvertToJSArray2D(VBArray , ArrayName)

Dim vb2jsRow, vb2jsCol , vb2jsStr, vb2jsi, vb2jsj
vb2jsRow = Ubound(VBArray,1)
vb2jsCol = Ubound(VBArray,2)
%>
<SCRIPT LANGUAGE = 'JAVASCRIPT' >
var vb2jsi,vb2jsj
<%=ArrayName%> = new Array(<%=vb2jsRow+1%>);
for (vb2jsi=0; vb2jsi < <%=vb2jsRow+1%>; vb2jsi++)
{
    <%=ArrayName%>[vb2jsi] = new Array(<%=vb2jsCol+1%>)
for (vb2jsj=0; vb2jsj < <%=vb2jsCol+1%>; vb2jsj++) <%=ArrayName%>[vb2jsi][vb2jsj] = " "
}
</SCRIPT>
<%
Response.Write("<SCR"&"IPT LANGUAGE = 'JAVASCRIPT' >"&chr(13))
for vb2jsi=0 to vb2jsRow
for vb2jsj=0 to vb2jsCol
    vb2jsstr = "VBArray("&vb2jsi&","&vb2jsj&")"
%>    
    <%=ArrayName%>[<%=vb2jsi%>][<%=vb2jsj%>] = "<%=trim(eval(vb2jsstr))%>"
<%
Next
Next
Response.Write("</SCR"&"IPT>")
End Function

%>

blog comments powered by Disqus
ASP ARTICLES

- Using MySQL with ASP
- ADO for the Beginner
- ADO.NET 101: Data Rendering with a DataGrid ...
- Introducing SoftArtisans OfficeWriter 3.0 En...
- Getting Remote Files With ASP
- The Real Basics of Functions in ASP
- Enhancing Readability with ASP
- Mimicking PHP's String Formatting Functions
- Windows Server Hacks 12, 77, and 98
- How to Sort a Multi-Dimensional Array
- Developing an Information Management Tool wi...
- What are Active Server Pages?
- Getting Remote Pages with ASP
- FTP’ing Files with ASP
- Apply Single-Sign-On to Your Application

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 7 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials