John MacIntyre wrote:
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.
-----------------------------------------------------
.. 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