ASP variables concatenated in js function

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

    ASP variables concatenated in js function

    Howdy ... having trouble here

    I have 3 session variables in ASP.

    Session("iCount 1")
    Session("iCount 2")
    Session("iCount 3")

    I need to concatenate these in a javascript function.

    <script type="text/javascript">
    function alertIt(){
    var sessionsArray = new Array();
    for(var i=0;i<3;i++){
    sessionsArray[i] = eval('<%'+'=Ses sion("iCount'+i +'")%>');
    alert(sessionsA rray[i]);
    }
    }
    </script>

    Doesn't work. I've tried all kinds of syntax but am in need of some advanced
    knoweledge at this point.

    David

    PS: This works below, a straightforward string of one of the session vars.

    <script type="text/javascript">
    function alertIt(){
    var theSess = '<%Response.Wri te(Session("iCo unt2"))%>';
    alert(theSess);
    }
    </script>



  • McKirahan

    #2
    Re: ASP variables concatenated in js function

    "David" <right@dd.com > wrote in message
    news:%A31e.1393 7$wL6.13581@trn ddc03...[color=blue]
    > Howdy ... having trouble here
    >
    > I have 3 session variables in ASP.
    >
    > Session("iCount 1")
    > Session("iCount 2")
    > Session("iCount 3")
    >
    > I need to concatenate these in a javascript function.
    >
    > <script type="text/javascript">
    > function alertIt(){
    > var sessionsArray = new Array();
    > for(var i=0;i<3;i++){
    > sessionsArray[i] = eval('<%'+'=Ses sion("iCount'+i +'")%>');
    > alert(sessionsA rray[i]);
    > }
    > }
    > </script>
    >
    > Doesn't work. I've tried all kinds of syntax but am in need of some[/color]
    advanced[color=blue]
    > knoweledge at this point.
    >
    > David
    >
    > PS: This works below, a straightforward string of one of the session vars.
    >
    > <script type="text/javascript">
    > function alertIt(){
    > var theSess = '<%Response.Wri te(Session("iCo unt2"))%>';
    > alert(theSess);
    > }
    > </script>[/color]


    An alternative is:

    <%@ Language="VBScr ipt" %>
    <%
    Session("iCount 1") = 1
    Session("iCount 2") = 2
    Session("iCount 3") = 3
    Dim i, a(), s
    For i = 0 To 2
    ReDim Preserve a(i)
    a(i) = Session("iCount "&i+1)
    Next
    s = Join(a,",")
    %>
    <html>
    <head>
    <title>Sessions .asp</title>
    <script type="text/javascript">
    function alertIt() {
    var sessionsArray = new Array(<%=s%>);
    for (var i=0; i<3; i++) {
    alert(sessionsA rray[i]);
    }
    }
    </script>
    </head>
    <body onload="alertIt ()">
    </body>
    </html>


    Comment

    • David

      #3
      Re: ASP variables concatenated in js function

      Thanks, very nice. That does work however the session variables in this case
      are strings. I found that I can get the string by modifying this..

      Session("iCount 1") = "number 1"
      Session("iCount 2") = "number 2"
      Session("iCount 3") = "number 3"

      var sessionsArray = new Array(<%=s%>);
      to this...
      var sessionsArray = new Array('<%=s%>') ;

      but then the alert brings up the array joined on the first alert, and the
      following 2 alerts come up as undefined.

      number1,number2 ,number3

      What do you think?

      David



      "McKirahan" <News@McKirahan .com> wrote in message
      news:Npydna6SJM MSUtnfRVn-vQ@comcast.com. ..[color=blue]
      > "David" <right@dd.com > wrote in message
      > news:%A31e.1393 7$wL6.13581@trn ddc03...[color=green]
      > > Howdy ... having trouble here
      > >
      > > I have 3 session variables in ASP.
      > >
      > > Session("iCount 1")
      > > Session("iCount 2")
      > > Session("iCount 3")
      > >
      > > I need to concatenate these in a javascript function.
      > >
      > > <script type="text/javascript">
      > > function alertIt(){
      > > var sessionsArray = new Array();
      > > for(var i=0;i<3;i++){
      > > sessionsArray[i] = eval('<%'+'=Ses sion("iCount'+i +'")%>');
      > > alert(sessionsA rray[i]);
      > > }
      > > }
      > > </script>
      > >
      > > Doesn't work. I've tried all kinds of syntax but am in need of some[/color]
      > advanced[color=green]
      > > knoweledge at this point.
      > >
      > > David
      > >
      > > PS: This works below, a straightforward string of one of the session[/color][/color]
      vars.[color=blue][color=green]
      > >
      > > <script type="text/javascript">
      > > function alertIt(){
      > > var theSess = '<%Response.Wri te(Session("iCo unt2"))%>';
      > > alert(theSess);
      > > }
      > > </script>[/color]
      >
      >
      > An alternative is:
      >
      > <%@ Language="VBScr ipt" %>
      > <%
      > Session("iCount 1") = 1
      > Session("iCount 2") = 2
      > Session("iCount 3") = 3
      > Dim i, a(), s
      > For i = 0 To 2
      > ReDim Preserve a(i)
      > a(i) = Session("iCount "&i+1)
      > Next
      > s = Join(a,",")
      > %>
      > <html>
      > <head>
      > <title>Sessions .asp</title>
      > <script type="text/javascript">
      > function alertIt() {
      > var sessionsArray = new Array(<%=s%>);
      > for (var i=0; i<3; i++) {
      > alert(sessionsA rray[i]);
      > }
      > }
      > </script>
      > </head>
      > <body onload="alertIt ()">
      > </body>
      > </html>
      >
      >[/color]


      Comment

      • David

        #4
        Re: ASP variables concatenated in js function

        This seems to work as I need it but I'm sure there's a more elegant method.
        If not thanks, you have helped me alot.



        <%@ Language="VBScr ipt" %>
        <%
        Session("iCount 1") = "number 1"
        Session("iCount 2") = "number 2"
        Session("iCount 3") = "number 3"
        Dim i, a(), s, b
        For i = 0 To 2
        ReDim Preserve a(i)
        a(i) = Session("iCount "&i+1)
        Next
        s = Join(a,"~")
        %>
        <html>
        <head>
        <title>Sessions .asp</title>
        <script type="text/javascript">
        function alertIt() {
        var sessionsArray = new Array('<%=s%>') ;
        var sessionsArraySp lit = sessionsArray.t oString().split ("~");
        for (var i=0; i<sessionsArray Split.length; i++){
        alert(sessionsA rraySplit[i]);
        }
        }
        </script>
        </head>
        <body onload="alertIt ()">
        </body>
        </html>


        Comment

        • McKirahan

          #5
          Re: ASP variables concatenated in js function

          "David" <right@dd.com > wrote in message
          news:Tr41e.2598 4$oa6.22571@trn ddc07...[color=blue]
          > Thanks, very nice. That does work however the session variables in this[/color]
          case[color=blue]
          > are strings. I found that I can get the string by modifying this..
          >
          > Session("iCount 1") = "number 1"
          > Session("iCount 2") = "number 2"
          > Session("iCount 3") = "number 3"
          >
          > var sessionsArray = new Array(<%=s%>);
          > to this...
          > var sessionsArray = new Array('<%=s%>') ;
          >
          > but then the alert brings up the array joined on the first alert, and the
          > following 2 alerts come up as undefined.
          >
          > number1,number2 ,number3
          >
          > What do you think?
          >
          > David[/color]

          [snip]

          Try:

          var sessionsArray = "<%=s%>".split( ",");


          Comment

          • David

            #6
            Re: ASP variables concatenated in js function

            > [snip][color=blue]
            >
            > Try:
            >
            > var sessionsArray = "<%=s%>".split( ",");[/color]


            Your a genuis :-)

            David


            Comment

            • humbads

              #7
              Re: ASP variables concatenated in js function

              If you always only have three variables, why use an array? Why not
              something straightforward like this? (Note the ASP portion is in
              VBScript below. Change ampersand to plus sign for Javascript.)

              var sessConcatenate d = '<%
              Response.write( Session("iCount 1") & ", ")
              Response.write( Session("iCount 2") & ", ")
              Response.write( Session("iCount 3"))
              %>';
              alert(sessConca tenated);

              Comment

              • David

                #8
                Re: ASP variables concatenated in js function

                It's a little more complicated than that, The example I gave was extremely
                condensed for legibility here in the forum. The number of sessions will be
                different for every user and they could get quite large.

                David





                "humbads" <humbads@gmail. com> wrote in message
                news:1111818087 .192641.6940@l4 1g2000cwc.googl egroups.com...[color=blue]
                > If you always only have three variables, why use an array? Why not
                > something straightforward like this? (Note the ASP portion is in
                > VBScript below. Change ampersand to plus sign for Javascript.)
                >
                > var sessConcatenate d = '<%
                > Response.write( Session("iCount 1") & ", ")
                > Response.write( Session("iCount 2") & ", ")
                > Response.write( Session("iCount 3"))
                > %>';
                > alert(sessConca tenated);
                >[/color]


                Comment

                • Evertjan.

                  #9
                  Re: ASP variables concatenated in js function

                  David wrote on 26 mrt 2005 in comp.lang.javas cript:
                  [color=blue]
                  > "humbads" <humbads@gmail. com> wrote in message
                  > news:1111818087 .192641.6940@l4 1g2000cwc.googl egroups.com...[color=green]
                  >> If you always only have three variables, why use an array? Why not
                  >> something straightforward like this? (Note the ASP portion is in
                  >> VBScript below. Change ampersand to plus sign for Javascript.)
                  >>
                  >> var sessConcatenate d = '<%
                  >> Response.write( Session("iCount 1") & ", ")
                  >> Response.write( Session("iCount 2") & ", ")
                  >> Response.write( Session("iCount 3"))
                  >> %>';
                  >> alert(sessConca tenated);
                  >>[/color]
                  >[/color]
                  [color=blue]
                  > It's a little more complicated than that, The example I gave was
                  > extremely condensed for legibility here in the forum. The number of
                  > sessions will be different for every user and they could get quite
                  > large.[/color]

                  If you want to use a clientside array:

                  =============== ========

                  <%@ language="vbscr ipt"%>
                  <% ' Using serverside asp-vbscript and clientside javascript: %>

                  <script type='text/javascript'>

                  clientsidearray = new Array(<%
                  for sessionvarcount =1 to maxsessionvar
                  Response.write "'" & Session("iCount "&sessionvarcou nt) & "'"
                  if sessionvarcount <maxsessionva r then Response.write ","
                  next
                  %>);

                  alert( clientsidearray .join('#') );

                  </script>

                  =============== ========

                  <%@ language="javas cript"%>
                  <% // Using serverside asp-jscript and clientside javascript: %>

                  <script type='text/javascript'>

                  clientsidearray = new Array(<%
                  for(sessionvarc nt=1;sessionvar cnt<=maxsession var;sessionvarc nt++){
                  Response.write( "'" + Session("iCount "+sessionvarcnt ) + "'");
                  if(sessionvarcn t<maxsessionvar )Response.write (",");
                  };
                  %>);

                  alert( clientsidearray .join('#') );

                  </script>

                  =============== =========

                  not tested


                  --
                  Evertjan.
                  The Netherlands.
                  (Replace all crosses with dots in my emailaddress)

                  Comment

                  • David

                    #10
                    Re: ASP variables concatenated in js function

                    I tested it and it does work .. interesting.


                    David



                    [color=blue]
                    > If you want to use a clientside array:
                    >
                    > =============== ========
                    >
                    > <%@ language="vbscr ipt"%>
                    > <% ' Using serverside asp-vbscript and clientside javascript: %>
                    >
                    > <script type='text/javascript'>
                    >
                    > clientsidearray = new Array(<%
                    > for sessionvarcount =1 to maxsessionvar
                    > Response.write "'" & Session("iCount "&sessionvarcou nt) & "'"
                    > if sessionvarcount <maxsessionva r then Response.write ","
                    > next
                    > %>);
                    >
                    > alert( clientsidearray .join('#') );
                    >
                    > </script>
                    >
                    > =============== ========
                    >
                    > <%@ language="javas cript"%>
                    > <% // Using serverside asp-jscript and clientside javascript: %>
                    >
                    > <script type='text/javascript'>
                    >
                    > clientsidearray = new Array(<%
                    > for(sessionvarc nt=1;sessionvar cnt<=maxsession var;sessionvarc nt++){
                    > Response.write( "'" + Session("iCount "+sessionvarcnt ) + "'");
                    > if(sessionvarcn t<maxsessionvar )Response.write (",");
                    > };
                    > %>);
                    >
                    > alert( clientsidearray .join('#') );
                    >
                    > </script>
                    >
                    > =============== =========
                    >
                    > not tested
                    >
                    >
                    > --
                    > Evertjan.
                    > The Netherlands.
                    > (Replace all crosses with dots in my emailaddress)
                    >[/color]


                    Comment

                    Working...