简体   繁体   English

如何在Safari中将Javascript对象传递给Java applet?

[英]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. 我正在尝试从Javascript调用Java applet,但是当我传递一个对象时,Java applet只接收null。 This works in IE and Firefox but not in Safari 4 (tried OSX and Windows version, neither seem to work). 这适用于IE和Firefox,但不适用于Safari 4(尝试过OSX和Windows版本,似乎都不起作用)。

The Java applet method I'm calling might look like: 我正在调用的Java applet方法可能如下所示:

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

And the HTML/Javascript that calls it: 以及调用它的HTML / Javascript:

<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. NullPointerException是Safari中的结果,因为Java applet中的“someobject”参数为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. 同样,这适用于Windows,OSX和Linux中的Firefox和IE,但在Safari中,sampleFunction()将传递一个null参数。

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. 如果我得到窗口对象并从Java调用Javascript,我传递的任何对象都不起作用。 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). 例如,我可以传递一个字符串数组,在Javascript中我得到一个[Ljava.lang.String类型对象但我无法访问其中的任何内容(长度未定义且数组运算符不起作用)。

Is Safari just broken or what? Safari只是破了还是什么? Any ideas? 有任何想法吗?

If you can, I'd suggest serializing the object to a JSON string. 如果可以,我建议将对象序列化为JSON字符串。 You can snag json2.js from json.org and use json.stringify(obj) sending it to your Java applet. 你可以从json.org获取json2.js并使用json.stringify(obj)将它发送到你的Java applet。 On the java side, you should be able to deserialize the JSON string to a native object. 在java方面,您应该能够将JSON字符串反序列化为本机对象。 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. 这种方法的缺点是,您可能需要一种方法来指定从Java端运行的回调方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM