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.

Member Avatar for stbuchok

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)?

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.

Member Avatar for stbuchok

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

(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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.