Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 349 Vote(s) - 3.57 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Javascript array to VBScript

#1
I have an array of objects built using Javascript and I need to read it using VBScript (as in the example below). I cannot find a way to loop through the array in my VbScript code as the `myArray` object.

The example is a simplification of my problem. I cannot change the default language of the page. The `myArray` object must be built using javascript. The array must be output using inline vbscript.

<%@ Language="VBScript" %>

<script language="javascript" runat="server">

var myArray = [
{
name: "object 1"
},
{
name: "object 2"
},
{
name: "object 3"
}
];

</script>

<%
Response.Write(myArray) ' [object Object],[object Object],[object Object]
'Response.Write(myArray(0)) ' ERROR
'Response.Write(myArray[0]) ' ERROR
Response.Write(myArray.[0]) ' [object Object]
Response.Write(myArray.[0].name) ' object 1
Response.Write(VarType(myArray)) ' 8
Response.Write(myArray.length) ' 3
Response.Write(VarType(myArray.[0])) ' 8
Response.Write(VarType(myArray.[0].name)) ' 8
Response.Write(TypeName(myArray)) ' JScriptTypeInfo
Response.Write(TypeName(myArray.[0])) ' JScriptTypeInfo

' ERROR
' Type mismatch: 'UBound'
'Response.Write(UBound(myArray))

' ERROR
' Object doesn't support this property or method: 'myArray.i'
'Dim i
'For i = 0 To myArray.length - 1
' Response.Write(myArray.[i])
'Next
%>
Reply

#2
Don't use arrays. Use a dictionary object as below.

<%@ Language="VBScript" %>

<script language="javascript" runat="server">

var myArray = [
{
name: "object 1"
},
{
name: "object 2"
},
{
name: "object 3"
}
];

var myDictionary = Server.CreateObject("Scripting.Dictionary");

for (var myArrayIndex = 0; myArrayIndex < myArray.length; myArrayIndex++) {
myDictionary.Add(myArrayIndex, myArray[myArrayIndex]);
}

</script>

<%
Dim i
For i = 0 To UBound(myDictionary.Keys)
Response.Write(myDictionary.Item(i).name)
Next
%>
Reply

#3
It seems the JScript array methods are still available via VBScript:

<script language="javascript" runat="server">
var myArray = [
{
name: "object 1"
},
{
name: "object 2"
},
{
name: "object 3"
}
];
</script>

<%
Do While myArray.length > 0
response.write myArray.shift().name
response.write "<br>"
Loop
%>
Reply

#4
You can delegate the printing to a JScript function:

<%@ Language="VBScript" %>
<script language="javascript" runat="server">
var myArray = [
{
name: "object 1"
},
{
name: "object 2"
},
{
name: "object 3"
}
];
</script>

<%
'// vbscript
Response.Write(TypeName(myArray)) ' JScriptTypeInfo

Response.Write printArr(myArray)

'// more vbscript
%>

<script language="javascript" runat="server">
function printArr(arr)
{
for (var i = 0; i < myArray.length; i++)
{
Response.Write("<br />index " + i + " = " + myArray[i].name);
}
}
</script>
Reply

#5
A little late to the party, but you can add a custom method to the standard javascript Array. This method will then also be available in VBscript.
So add this code to your script:

// add an Item() method to the standard array object so we can iterate arrays in VBscript.
Array.prototype.Item = function(idx) {
return this[idx];
};

And you can use:

myArray.Item(0)

to get to one of the items in the array in VBscript.

HTH
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through