server to browser transference
hi everybody
i need for mi hobbie web app a transference between a java array and a javascript array. this is what i have tried to accomplish but it doesnt seem to work well:
<script type="text/javascript">
var arrayJS=new array();
<% String[] javaArray=(String[])request.getAttribute("names");%>
arrayJS=<%=javaArray%>;
for(var i=0;i<arrayJS.length;i++)
//data processing
any help would be so much appreciated
Re: server to browser transference
As you've seen this isn't the way to go about it. How is the request parameter "names" formatted? Based on your code it sounds like it would be something like
names=Bob&names=Sue&names=Barf
is this correct? That's kind of a weird URL but it can be dealt with entirely in JavaScript:
Code :
var arrayJS=new array();
var searchString = document.location.search;
// strip off the leading '?'
searchString = searchString.substring(1);
var nvPairs = searchString.split("&");
for (i = 0; i < nvPairs.length; i++) {
var nvPair = nvPairs[i].split("=");
if( nvPair[0] === 'names' ) {
arrayJS.push( nvPair[1] );
}
}
This code was derived from here.