views:

451

answers:

1

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?

+1  A: 

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.

Tracker1
Yeah, that's exactly what I have been doing. I have a mechanism whereby callbacks get a unique name and can be called using their text-based name. With that said, it's still a stupid inefficient hack due to one single broken browser.
CR
Every finished piece of javascript contains at least one "stupid and inefficient hack due to one single broken browser". Unless you don't care about those millions of users still using IE6...
Dustin Fineout