• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

passing java array to javascript

 
sihani rani
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have String array in jsp ..
how can i pass that String array to java Script function


this is my java code:


<%

String[] s=new String[10];

s[0]="apple";
s[1]= "orange";
s[2]="banana";
s[3]="mango";



%>
<body onload="???"> // how can i pass string array 's' to onload function


this is my java script code


function(???) // how can i get String array 's' from onload function
{



}



please answer me..

thanks in advance..





 
Nilesh Miskin
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Retrieving template text with JavaScript sounds a weird requirement to me. You can evaluate other design options to fulfill your goals.
One more thing,
String[] s=new String[];

Above statement won't compile as you haven't specified the dimension(size of the array).
 
Sean Clark
Rancher
Posts: 377
Android Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,

You can't pass the array directly you'd need to translate it into into javascript. You could use JSON or create a custom taglib to do it for you. Also having code scriptlets in your JSP is not really a good idea.

Sean
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's no reason to pass the Java array to the JavaScript function: define the array in JavaScript.

If that's not the problem you're *actually* trying to solve, then TellTheDetails.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64990
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please be sure to use code tags when posting code to the forums. Unformatted or unindented code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please click this link ⇒ UseCodeTags ⇐ for more information.

Properly indented and formatted code greatly increases the probability that your question will get quicker, better answers.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic