File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes passing java array to javascript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "passing java array to javascript" Watch "passing java array to javascript" New topic
Author

passing java array to javascript

sihani rani
Ranch Hand

Joined: Jun 22, 2010
Posts: 32
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

Joined: Jun 17, 2010
Posts: 44
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).


Nilesh Miskin
Sean Clark
Rancher

Joined: Jul 15, 2009
Posts: 377

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


I love this place!
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Jan 10, 2002
Posts: 61415
    
  67

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: passing java array to javascript