5

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?

CR.
  • 334
  • 2
  • 11
  • Did you try this in google chrome? That also uses WebKit, it may be a webkit issue. – Mech Feb 11 '10 at 19:59
  • Works fine in Chrome for Windows. Does not work in Chrome for Linux or OSX. – CR. Feb 11 '10 at 20:08
  • Don't know whether this anything to do with it or not: can you swap out the 'applet' tag for the ' – monojohnny Feb 12 '10 at 12:59
  • This similar ? http://stackoverflow.com/questions/1211342/signed-java-applet-doesnt-get-permissions-in-safari – monojohnny Feb 12 '10 at 13:00
  • It's not a permissions problem as I have seen that and it's a completely different error. Using object tags makes no difference. – CR. Feb 15 '10 at 22:12
  • Did you get any resolution to this? I'm also seeing strange behaviour in Safari where it won't even pass strings: http://stackoverflow.com/questions/6071601 – edoloughlin May 20 '11 at 12:15

1 Answers1

1

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
  • 19,103
  • 12
  • 80
  • 106
  • 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. Feb 16 '10 at 14:21
  • 1
    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... – defines Mar 05 '10 at 14:27
  • 1
    Well, we _shouldn't_ have to care about a 10 year old browser. – Matthijs Bierman Nov 04 '10 at 14:44
  • @matthijs unfortunately "shouldn't" and "do" are different things.. also, it's not quite 10 yet (Not until August of 2011). – Tracker1 Nov 04 '10 at 19:14