By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,838 Members | 2,222 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,838 IT Pros & Developers. It's quick & easy.

Pass a String array in a javascript

P: n/a
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
Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Sunny wrote:
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.
Jul 23 '05 #2

P: n/a
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

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

P: n/a
Sunil Kamath wrote:
Do u mean writing the entire javascript function
in jsp scriplet tags?

<snip>

Where did I say anything about functions?

Richard.
Jul 23 '05 #4

P: n/a
Sunil Kamath wrote:
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
Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.