Converting a JS array into a VBArray

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John MacIntyre

    Converting a JS array into a VBArray

    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



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

    -----------------------------------------------------
    <%@ Language=VBScri pt %>
    <html>
    <script language=javasc ript>
    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(nLoopCn t)
    Response.Write "<P>" & oName.firstName & " " & oName.lastName &
    "</P>"
    next
    %>
    </body>
    </html>





  • Steve van Dongen

    #2
    Re: Converting a JS array into a VBArray

    On Sun, 28 Sep 2003 20:51:00 -0400, "John MacIntyre"
    <Please@reply.t o.group.thx> wrote:
    [color=blue]
    >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=VBScri pt %>
    ><html>
    ><script language=javasc ript>
    > 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(nLoopCn t)
    > Response.Write "<P>" & oName.firstName & " " & oName.lastName &
    >"</P>"
    > next
    > %>
    ></body>
    ></html>[/color]

    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=some data or submit a form. You could do something like...
    var sUrl = "somepage.asp?n ame1=Joe,Doe&na me2=Sally,Strut hers";
    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

    Comment

    • John MacIntyre

      #3
      Re: Converting a JS array into a VBArray .. correct sample code.

      "Steve van Dongen" <stevevd@hotmai l.com> wrote in message
      news:039fnv0ngf jqne5f31k3n96ms nm431or36@4ax.c om...[color=blue]
      > On Sun, 28 Sep 2003 20:51:00 -0400, "John MacIntyre"
      > <Please@reply.t o.group.thx> wrote:
      >[color=green]
      > >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.
      > >-----------------------------------------------------[/color][/color]
      [color=blue]
      >
      > 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?
      >[/color]

      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


      -----------------------------------------------------------------
      <%@ Language=VBScri pt %>
      <html>
      <script language=javasc ript 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(nLoopCn t)
      Response.Write "<P>" & oName.firstName & " " & oName.lastName & "</P>"
      next
      %>
      </body>
      </html>





      Comment

      • Dom Leonard

        #4
        Re: Converting a JS array into a VBArray .. correct sample code.

        John MacIntyre wrote:
        [color=blue][color=green][color=darkred]
        >>>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.
        >>>-----------------------------------------------------[/color][/color]
        >
        >
        > .. I forgot runat=server in the sample I provided.
        >
        > But it still doesn't work.[/color]


        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=VBScri pt %>
        <html>
        <script language=javasc ript 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(myArra y) = " & isArray(myArray ) & "<BR>")

        for nLoopCnt = 0 to 1 'keep lookup in bounds
        Set oName = myArray.get(nLo opCnt) '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="VBSCR IPT"> 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

        Comment

        Working...