开发者

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 Javascr开发者_StackOverflow社区ipt 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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜