Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 :)

like image 465
Sébastien Nussbaumer Avatar asked Feb 20 '09 16:02

Sébastien Nussbaumer


People also ask

Does Firefox support Java applets?

You can enable both Java and JavaScript in the desktop version of Firefox.

How do I enable Java applets on my Mac?

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.

Which browsers still support Java applets?

Obviously, Internet Explorer is the go-to browser that still supports Java applets natively.


2 Answers

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" />
like image 66
Paulo Lopes Avatar answered Oct 03 '22 07:10

Paulo Lopes


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.

like image 37
Tristan Juricek Avatar answered Oct 03 '22 07:10

Tristan Juricek