Forum Moderators: open

Message Too Old, No Replies

Returning array to JavaScript from Java applet

Problem assigning Java array to JavaScript var

         

TerryS

3:23 pm on Sep 27, 2005 (gmt 0)

10+ Year Member



Hi,

I have a question for which I cannot find an answer and would be very grateful for some help.

I have a Java applet within an HTML page and I can call the applet's methods from JavaScript without a problem when the page is viewed in Internet Explorer 6.0. If an applet method returns any Java primitive data type then the value can be assigned to a JavaScript variable and displayed (and this is also true if the applet method returns a Java String). However, if an applet method returns a Java array then the JavaScript variable to which it is assigned is always valueless.

So, with Internet Explorer 6.0 is it actually possible to have an applet method that returns a Java array to JavaScript? If so, could someone please provide a simple example. Many thanks.

Terry.

Bernard Marx

4:31 pm on Sep 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




I have no experience of any interface between Java & Javascript. I'd guess that, although the string forms in each language are internally similar enough, the internal difference between Java and Javascript arrays is probably enormous. That's not to say there isn't a formal way to do this.

JScript (as opp to Javascript) also doesn't seem to have the inbuilt Java interface objects.

If you can't find any "official" way, there could be a work around (or two).

1) Get your applet to output the array as a delimited string (Here I've used colons, but it could be any string), then get JS to split the string. This is only good for strings.

var output = "apple:orange:banana"
var outputArray = output.split(':');

2) Get the applet to return a string in the form of an array literal. Then eval it. This covers string, number and boolean members.


var output = "['apple',2,false]";
var outputArray = eval(output);

TerryS

2:37 pm on Sep 28, 2005 (gmt 0)

10+ Year Member



Thank you for your suggestions Bernard. I had considered this approach but was hoping to avoid conversion to a string because one of the arrays I wish to return to JavaScript from the Applet is a byte array which I then wish to pass to an ActiveX method which will accept the byte array as a variant data type. Thus ideally I would like the JavaScript to accept the Java byte array as a sort of 'blob', the contents of which I do not not need to access in the JavaScript itself.

Terry.