Pass a String array in a javascript

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

    Pass a String array in a javascript

    Hi,
    I am at present working on a jsp page which has a single text field.
    Upon entering a value in the field, i am trigerring an onChangeEvent()
    which calls a method of javascript. I am also dynamically creating an
    array in the jsp page and want to pass this array (by reference) to
    the javascript function.
    All I want to do in the javascript function is to check whether the
    input value matches with any of the array elements. However, i have
    not been able to pass the String array in the javascript by reference.
    Any help/suggestion would be appreciated.

    cheers
  • Richard Cornford

    #2
    Re: Pass a String array in a javascript

    Sunny wrote:[color=blue]
    > I am at present working on a jsp page which has a single text field.
    > Upon entering a value in the field, i am trigerring an onChangeEvent()
    > which calls a method of javascript. I am also dynamically creating an
    > array in the jsp page and want to pass this array (by reference) to
    > the javascript function.[/color]

    Then you have a lot to learn about the separation of server-side
    scripting an client-side scripting. As the two execute at different
    times and in different places passing an array by reference is
    meaningless.
    [color=blue]
    > All I want to do in the javascript function is to check whether the
    > input value matches with any of the array elements. However, i have
    > not been able to pass the String array in the javascript by reference.
    > Any help/suggestion would be appreciated.[/color]

    (apart from indirect tricks with XMLHTTP requests and the like, which
    introduce significant unreliability's ) The obvious way of getting a Java
    array from a JSP to any script running in the browser based on the HTTP
    response sent form the JSP to the browser, is to have the JSP write the
    array into its output in the form of a javascript Array literal. Thus
    the data becomes available to the script in the browser.

    Richard.


    Comment

    • Sunil Kamath

      #3
      Re: Pass a String array in a javascript

      Do u mean writing the entire javascript function in jsp scriplet tags?

      As in

      <% out.println("<S CRIPT language=javasc ript>");
      out.println("fu nction fname(){");
      ..............
      ..............
      out.println("}</SCRIPT>");
      %>

      Wont this print the words (between the quotes) on the jsp page?

      cheers

      *** Sent via Developersdex http://www.developersdex.com ***
      Don't just participate in USENET...get rewarded for it!

      Comment

      • Richard Cornford

        #4
        Re: Pass a String array in a javascript

        Sunil Kamath wrote:[color=blue]
        > Do u mean writing the entire javascript function
        > in jsp scriplet tags?[/color]
        <snip>

        Where did I say anything about functions?

        Richard.


        Comment

        • Thomas 'PointedEars' Lahn

          #5
          Re: Pass a String array in a javascript

          Sunil Kamath wrote:
          [color=blue]
          > Do u mean writing the entire javascript function in jsp scriplet tags?
          >
          > As in
          >
          > <% out.println("<S CRIPT language=javasc ript>");
          > out.println("fu nction fname(){");
          > .............
          > .............
          > out.println("}</SCRIPT>");
          > %>[/color]

          No, as in

          %>
          <script type="text/javascript>"
          function fname()
          {
          ...
          }
          </script>
          <%

          of course.
          [color=blue]
          > Wont this print the words (between the quotes) on the jsp page?[/color]

          It will, however you missed the point. Richard meant something like

          %>
          <script type="text/javascript>"
          var jsArray = ["<%
          String[] javaArray = {"foo", "bar"};
          out.println(jav aArray.join("\" , \""));
          %>"];
          </script>
          <%

          which could generate

          <script type="text/javascript>"
          var jsArray = ["foo", "bar"];
          </script>


          HTH

          PointedEars

          Comment

          Working...