Are Java Applets unable to communicate with javascript within Firefox on Mac OS?

Sébastien Nussbaumer picture Sébastien Nussbaumer · Feb 20, 2009 · Viewed 10k times · Source

I have a java applet running in a browser that is calling some javascript functions and expecting a result from theses functions. This is working with the following configurations :

  • Internet Explorer
  • FireFox / Windows
  • Safari / Mac

BUT It's not working with Firefox on MAC OS

The source of the problem seems to be the win.eval calls that always return null. I tested this with Firefox 3.0.6 on a Mac OS X 10.4.11

A bit of code :

JSObject win = (JSObject) JSObject.getWindow(this);
Object exp = win.eval("testfunc()");
System.out.println("exp = " + exp.toString());

This triggers a java.lang.NullPointerException (exp.toString()) statement). The testfunc javascript function just returns true.

I tried with win.call and got the same result.

My applet tag includes the mayscript and scriptable attributes.


I found the answer thanks to Tristan. Testing his solution I created a really simple test that could work and worked my way to find the culprit. I was sure I did my tests with an empty testfunc() that just returned true, but I probably didn't because in that case it DOES work. The real problem here was that the function called a public method of the applet. Liveconnect doesn't seem to be able to handle that case in Firefox Mac.

Let me give you an example :

Java class :

public class MyApplet extends Applet {  
     public int getMyValue() {
         return 5;
     }

     public void somefunction() {
         JSObject win = (JSObject) JSObject.getWindow(this);
         Object exp = win.eval("jsfunc()");
         System.out.println("exp = " + exp.toString());
     }
}

And the javascript code :

function jsfunc() {
    var myApplet = document.getElementById("applet_id");
    return myApplet.getMyValue() + 5;
}

exp will be null in somefunction BECAUSE jsfunc calls the getMyValue() method of the applet. If you remove all calls to properties of the applet you're cool.
To solve my problem I decided to give all the values of the applet that I neeeded as parameters of my javascript function and I'm good now.
That may not be the case always, if the javascript changes the state of the applet ...I was lucky :)

Answer

Paulo Lopes picture Paulo Lopes · Feb 23, 2009

I haven't used the applet api in a while but if i recall correctly in order to allow an Applet to cann JS code you should enable the attribute mayscript in your applet tag or a param mayscript in the object tag notation.

For communication in the other way JS to Applet you should also use the scriptable attribute or parameter, for example:

<applet code="..." mayscript="true" />

This allows your applet to use script functions.

<applet code="..." scriptable="true" />