Pass a String array in a javascript

S

Sunny

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
 
R

Richard Cornford

Sunny said:
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.

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

(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.
 
S

Sunil Kamath

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

As in

<% out.println("<SCRIPT language=javascript>");
out.println("function fname(){");
..............
..............
out.println("}</SCRIPT>");
%>

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

cheers
 
T

Thomas 'PointedEars' Lahn

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

As in

<% out.println("<SCRIPT language=javascript>");
out.println("function fname(){");
.............
.............
out.println("}</SCRIPT>");
%>

No, as in

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

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

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

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

which could generate

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


HTH

PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top