Converting a JS array into a VBArray

J

John MacIntyre

Hi,

Can anybody give me a hint as to how to convert a javascript array into a
vbscript array?

BTW-it only needs to work in IE5 & 6

Thanks in advance,
John MacIntyre
VC++ / VB / ASP / Database Developer
http://www.johnmacintyre.ca


Here's some sample code I threw together to explain the problem.

-----------------------------------------------------
<%@ Language=VBScript %>
<html>
<script language=javascript>
function getJSArray()
{
var retArray new Array();

retArray[0] = new Object();
retArray[0].firstName = "Joe";
retArray[0].lastName = "Doe";
retArray[1] = new Object();
retArray[1].firstName = "Sally";
retArray[1].lastName = "Struthers";

return retArray;
}
</script>
<body>
<%
dim myArray()
dim nLoopCnt
dim oName

myArray = getJSArray()
for nLoopCnt = 0 to 3
oName = myArray(nLoopCnt)
Response.Write "<P>" & oName.firstName & " " & oName.lastName &
"</P>"
next
%>
</body>
</html>
 
S

Steve van Dongen

Hi,

Can anybody give me a hint as to how to convert a javascript array into a
vbscript array?

BTW-it only needs to work in IE5 & 6

Here's some sample code I threw together to explain the problem.

-----------------------------------------------------
<%@ Language=VBScript %>
<html>
<script language=javascript>
function getJSArray()
{
var retArray new Array();

retArray[0] = new Object();
retArray[0].firstName = "Joe";
retArray[0].lastName = "Doe";
retArray[1] = new Object();
retArray[1].firstName = "Sally";
retArray[1].lastName = "Struthers";

return retArray;
}
</script>
<body>
<%
dim myArray()
dim nLoopCnt
dim oName

myArray = getJSArray()
for nLoopCnt = 0 to 3
oName = myArray(nLoopCnt)
Response.Write "<P>" & oName.firstName & " " & oName.lastName &
"</P>"
next
%>
</body>
</html>

Your example code is running Javascript on the client-side and
VBScript on the server-side. Is that what you're trying to do or are
you writing ASP in both VBScript and JScript?

The only way to pass data from the client-side to the server-side is
to send a GET or POST request, i.e. navigate to a URL with
?something=somedata or submit a form. You could do something like...
var sUrl = "somepage.asp?name1=Joe,Doe&name2=Sally,Struthers";
window.location.href = sUrl;
and then loop through the parameters and values on the server-side,
something like...
Dim myArray()
i = 1
While Request("name" & i).Count != 0
myArray(i) = Split(Request("name" & i), ",")

(My VBScript is probably way off. I've tried to stay away from
learning VBScript.)

Regards,
Steve
 
J

John MacIntyre

Your example code is running Javascript on the client-side and
VBScript on the server-side. Is that what you're trying to do or are
you writing ASP in both VBScript and JScript?

You are right .. I forgot runat=server in the sample I provided.

But it still doesn't work.

Thanks in advance,
John MacIntyre
VC++ / VB / ASP / Database Developer
http://www.johnmacintyre.ca

-----------------------------------------------------------------
<%@ Language=VBScript %>
<html>
<script language=javascript runat=server>
function getJSArray()
{
var retArray = new Array();

retArray[0] = new Object();
retArray[0].firstName = "Joe";
retArray[0].lastName = "Doe";
retArray[1] = new Object();
retArray[1].firstName = "Sally";
retArray[1].lastName = "Struthers";

return retArray;
}
</script>
<body>
<%
dim myArray()
dim nLoopCnt
dim oName

myArray = getJSArray() '<-- gives 'Type Mismatch' error
for nLoopCnt = 0 to 3
oName = myArray(nLoopCnt)
Response.Write "<P>" & oName.firstName & " " & oName.lastName & "</P>"
next
%>
</body>
</html>
 
D

Dom Leonard

John said:
.. I forgot runat=server in the sample I provided.

But it still doesn't work.


Javascript arrays are an object type with additional features. My
testing shows that when accessed from VB, they are still objects rather
than VB's idea of an array.

Additionally
a) VB gave errors when attempting to retrieve object properties with
numeric property names (the javascript implementation of arrays), and
b) the Set keyword must be used when assigning obects in VB.

In workaround, I added a getter to the prototype for Array objects in
JScript, to allow retrieval of subscripted entries by function call.
This is called using zero based indexing, of course, taking care not to
exceed array dimensions. So the script tested in IE5 ended up as:

<%@ Language=VBScript %>
<html>
<script language=javascript runat=server>

// Define a getter for Array entries

Array.prototype.get = function(prop)
{
return this[prop];
}

function getJSArray()
{
var retArray = new Array();
retArray[0] = new Object();
retArray[0].firstName = "Joe";
retArray[0].lastName = "Doe";
retArray[1] = new Object();
retArray[1].firstName = "Sally";
retArray[1].lastName = "Struthers";

return retArray;
}
</script>
<body>
<%
dim myArray 'removed array-like declaration
dim nLoopCnt
dim oName

Set myArray = getJSArray() 'use Set for objects
' notice myArray is not a VBScript array:
Response.Write("isArray(myArray) = " & isArray(myArray) & "<BR>")

for nLoopCnt = 0 to 1 'keep lookup in bounds
Set oName = myArray.get(nLoopCnt) 'use .get for numeric lookup, and
'use Set for object assignment
Response.Write "<P>" & oName.firstName & " " & oName.lastName & "</P>"
next
%>
</body>
</html>

By removing the ASP tags, surrounding the VB between <SCRIPT
LANGUAGE="VBSCRIPT"> and </SCRIPT> tags, and changing "Response.Write"
to "Document.Write", it also checks out client side in IE6. I am not a
VBScript expert, however, so am unable say how typical this solution
might be.


HTH

Dom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,100
Messages
2,570,635
Members
47,243
Latest member
RustyPalin

Latest Threads

Top