Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a Javascript object to a Java applet in Safari?

I'm trying to call into a Java applet from Javascript but when I pass an object the Java applet only receives null. This works in IE and Firefox but not in Safari 4 (tried OSX and Windows version, neither seem to work).

The Java applet method I'm calling might look like:

public void sampleFunction(JSObject someobject) throws Exception
{
   someobject.call("somemethod");
}

And the HTML/Javascript that calls it:

<applet archive="Test.jar" code="Test.class" width="0" height="0" id="TestApplet" MAYSCRIPT></applet>
...
var testobject = { somemethod: function() {} };
document.TestApplet.sampleFunction(testobject);

NullPointerException is the result in Safari because the "someobject" parameter in the Java applet is null. Again, this works fine in Firefox and IE in Windows, OSX, and Linux but in Safari the sampleFunction() will get passed a null parameter.

Also, the reverse direction doesn't seem to work either. If I get the window object and call into the Javascript from Java any objects I pass don't work. For example, I can pass an array of Strings and in the Javascript I get a [Ljava.lang.String type object but I can't access anything in it (length is undefined and the array operators don't work).

Is Safari just broken or what? Any ideas?

like image 756
CR. Avatar asked Nov 14 '22 12:11

CR.


1 Answers

If you can, I'd suggest serializing the object to a JSON string. You can snag json2.js from json.org and use json.stringify(obj) sending it to your Java applet. On the java side, you should be able to deserialize the JSON string to a native object. This is probably the most reliable method of tranfering the data. The down side to this method, is you will likely need a means of specifying a callback method to run from the Java side.

like image 199
Tracker1 Avatar answered Dec 09 '22 18:12

Tracker1