4

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
%>
5
  • I cannot change the default language of the page - so just change it for the output part? Commented Jan 21, 2015 at 17:02
  • @AlexK. As I understand it I cannot write inline javascript if the default language of the page is vbscript. Commented Jan 21, 2015 at 21:43
  • Isnt you current JavaScript doing that via runat=server? Commented Jan 21, 2015 at 22:01
  • @AlexK. The <script language="javascript" runat="server"> seems to run before everything. If I were to put it at the bottom of my page with a Response.Write() the resulting text would appear at the top of my page. Commented Jan 21, 2015 at 23:57
  • Added an example of what I was talking about Commented Jan 22, 2015 at 12:31

4 Answers 4

8

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

Sign up to request clarification or add additional context in comments.

1 Comment

Great solution.
5

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
%>

Comments

0

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
%>

3 Comments

Scripting.Dictionary object is a big unneeded overhead, wouldn't recommend this approach.
@Lankymart I was considering deleting this post it but it is another way to achieve the desired outcome. The solution by Stephen is much better.
Fair enough, there is no need to remove it.
0

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>

1 Comment

In my original question I should have explained that my vbscript code does a lot more than simply printing out the objects name property. The array is a collection of search result objects with many properties and there are multiple inline vbscript blocks mixed with html to build a list of search results. Using Response.Write to build html is troublesome and results in code that is hard to maintain. The solution ""must"" allow inline vbscript to iterate through the array built in javascript.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.