0

Hi,
I want to pass String array to the javascript function when the page is getting loaded. Iam using jsp and javascript for doing this. But could not able to send the Stirng array to the javascript function.

<%! String msg[] = {"one","two","three"}; %>
<input type="button" name="save" value="Save" onclick="show(msg);" />

below is my javascript function:
   function show(msg)
    {
    alert("in show");
    alert("---" + msg.length);
    }

In my above code iam using onClick but my requirement is when the page gets loaded the String array have to load in the javascript function. I have tried in another ways also but its not getting displayed in the javascript function. Thanks in advance.

Regards.

3
Contributors
4
Replies
26
Views
4 Years
Discussion Span
Last Post by radhakrishna.p
0

You are trying to pass what I'm assuming is a server side variable as a javascript array. This isn't going to happen. You have to render it as javascript. What server side language are you using (it almost looks like .Net)?

0

I am getting the database values into jsp page and storing in an String array. That String array i want to pass to a javascript function.Iam using jsp i.e.,java for this.

In the below way i will store my records which iam getting from database in a String array.
(I just shown you the static way below)
<%! String msg[] = {"one","two","three"}; %>
When i click the button iam calling show(msg) function, i.e., i want to pass array variable "msg" 
to the javascript show function where i can use that array variable.
<input type="button" name="save" value="Save" onclick="show(msg);" />
below is my javascript function:
   function show(msg)
    {
    alert("in show");
    alert("---" + msg.length);
    }

Iam using jsp and javascript.How can i achieve this?
Thanks for your time.

0

Java is NOT javascript. They are 2 completely different things, not related in any way whatsoever (except the name)

0

(suggestion only:) why dont you try like this

Actually java script can access any of html input elements whereever you placed with in the page

if you try to place your string array values in any of the html elements like

input fields,divs,spans,and so on (based on our requiement we can hide these html elements also for not showing anything to user)

you can access your values diffinately from your java script.

lets take an example:

instead of sting array values make your string array as follows

<%

String my_str_values = "value1,value2,value3" // instead of string array you better to take string of values seperated by commas

%>

store this 'my_str_values' value in hidden type html input elements as shown bellow

<input type="hidden" name="my_val" id="my_val" value="<%= my_str_vaues%>"/>

so that this html input element is accessble to javascript

there you can use split() method to get your array back

you can do whatever you want.

try like this it may helpful

let me know if you have any doubts in my clarification

happy coding

Edited by radhakrishna.p: giving clarity on the solution

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.