How to pass java array to javascript function, and how to access it in javascript function ?

I want to access the array passed to javascript function
I am not sure whether I am doing it right way or not. I have tried following

I have java class:
public class Test
{
String str="abc";
String all[]=new String[3];
public String[] getDimension() 
{
	all[0]="India";
	all[1]="UK";
	all[2]="Japan";
	
	return all;
}

public String getValue()
{
return str;
}
}

Open in new window


I am calling above methods on jsp page inside script tags as follows
<script type="text/javascript"> 
 level_zero('<%=c1.all()%>',  '<%=c1.getValue()%>');
  </script> 

Open in new window


I have js file with function level_zero:
function level_zero(){
 for (var i = 0; i < 3; i++) {
         dimensions[i]=arguments[i];
    }

Open in new window

Pradip ShenolkarAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rrzCommented:
Please try this code.
package rrz;
public class Test
{
String str="abc";
String all[] = new String[3];
public String[] getDimension() 
{
	all[0]="India";
	all[1]="UK";
	all[2]="Japan";
	
	return all;
}

public String getValue()
{
return str;
}
}

Open in new window

my JSP
<%@ page import="rrz.Test" %>
<%
   Test c1 = new Test();
%>
<html>
   <head>
<script type="text/javascript"> 
var dimensions = [];
function level_zero(){
 for (var i = 0; i < 4; i++) {
         dimensions[i] = arguments[i];
    }
}
 level_zero('<%=c1.getDimension()[0]%>', '<%=c1.getDimension()[1]%>','<%=c1.getDimension()[2]%>',  '<%=c1.getValue()%>');
 alert(dimensions);
  </script> 
      </head>
 <body>
    testing
  </body>
</html>

Open in new window

0
rrzCommented:
0
Pradip ShenolkarAuthor Commented:
I want to pass whole array at once, because number of arguments to be passed are decided at runtime.
is it possible to do something like this :
level_zero('<%=c1.getDimension()%>',  '<%=c1.getValue()%>');

Open in new window

and later access array in js function.

It is possible by converting java array to javascript array.
Thats why i have posted this question:
http://www.experts-exchange.com/Programming/Languages/Java/J2EE/JSP/Q_28629014.html
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

rrzCommented:
It is possible by converting java array to javascript array.
Didn't I answer that question at
http://www.experts-exchange.com/Programming/Languages/Java/J2EE/JSP/Q_28617362.html#a40611486
0
rrzCommented:
I'll repost it here.
<html>
<head>
<script>
function validate()
  {
    var fruitArray = new Array();
<%
  String[] fruits = {"apple","peach","pear"};
  for(int i = 0; i < fruits.length; i++)
   {
    out.println("fruitArray[" + i + "]=\"" + fruits[i] + "\";");
   }
%>
  alert(fruitArray);
}
</script>
</head>
<body onload="validate()">
testing
</body>
</html>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Pradip ShenolkarAuthor Commented:
This is same code but with little modification.
Could you please tell how do I access values of fruitArray inside function call_me().
<html>
<head>
<script>

function call_me()
{
	alert(arguments[0])
}

function validate()
  {
    var fruitArray = new Array();
    var string;
<%
  String[] fruits = {"apple","peach","pear"};
  for(int i = 0; i < fruits.length; i++)
   {
    out.println("fruitArray[" + i + "]=\"" + fruits[i] + "\";");
   }
%>
 
  call_me(fruitArray);
}
</script>
</head>
<body onload="validate()">
testing
</body>
</html>
                                          

Open in new window

0
Pradip ShenolkarAuthor Commented:
ok I figured it out.
function call_me()
{
	alert(arguments[0][1])
}

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.