#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Bangalore
    Posts
    7
    Rep Power
    0

    Passing array from JSP to Javascript


    Hi,

    How can I pass an array generated in JSP to javascript?

    I got one solution :-
    Passing the values of the array individually to java script.

    Any idea other than this. Pls suggest.
  2. #2
  3. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    Since Javascript is run client side, you have to 'build' the array when writing the page. Something like:
    Code:
    <%
    String[] params = request.getParameterValues("someField");
    if(params != null) { %>
    <script>
    var params = new Array(<%
    for(int i = 0; i < params.length; i++) {
      out.print("\""+params[i]+"\"");
      if(i+1 < params.length) {
        out.print(",");
      }
    }
    %>);
    </script>
    <% } %>
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Bangalore
    Posts
    7
    Rep Power
    0
    Thanks Nemi,

    But is there any way to pass the array as a whole.

    Like by giving the array name alone.
  6. #4
  7. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    No. Java on the server and javascript on the client cannot communicate.

IMN logo majestic logo threadwatch logo seochat tools logo