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 :
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 :)
You can enable both Java and JavaScript in the desktop version of Firefox.
From the Tools menu, click Add-ons. In the left column, select Plugins. Click Java Applet plug-ins, and then, on the right, click Enable or Disable.
Obviously, Internet Explorer is the go-to browser that still supports Java applets natively.
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" />
Would it work via accessing one of the global objects on the screen? Ergo,
In JavaScript:
window.testfunc = function() { //... }
In Applet:
win.eval("window.testfunc()") // or maybe just win.eval("testfunc()")
That would be my experiment. But I've been calling "window.close()" on FF on Mac OS X, and that still works.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With