5 Replies Latest reply on Dec 12, 2002 4:20 PM by 843797

    Applet javascript communication.

      I have a html page with an applet and some javascript. The applet calls a method "doSomething()" in the script. The script calls another method in the applet:

      <APPLET CODE=somApplet          
      function doSomething(str)
        // this function is called by java applet
        // do something...
        str = str+"something";
        // doing an alert to conferm that this script function is running ok
        window.alert(str);// ok!
        // here I am trying to call a method in the applet:

      This works perfect in IE6, but not in Netscape 6. Why? No errors or exceptions. Nothing. The call just doesnt reach the applet in Netscape. pls help me out here!
      Ragnvald Barth
      Software enigneer
        • 1. Re: Applet javascript communication.
          Just adding a note:
          If I use a button:
               ONCLICK='theApplet.someMethod("javascript button clicked");'
               value="Call java from javascript"
          If I use a button like this, the applet method gets called from both Netscape and IE. The problem is only when I try to call the java Applet from a script method that was itself called by the java Applet.
          • 2. Re: Applet javascript communication.
            One thing you should tell us is how your Applet invokes JavaScript functions. This is the real problem, because the two browsers have completely different mechanisms to implement J(ava)Script: Microsoft's one is ActiveX based, while Netscape's is Java Based and changed heavily form 4.x to 6. Now, the way applets can invoke script code in HTML pages is not the same at all (different classes involved for IE and NS). Moreover, Netscape's <applet> tag has a "mayscript" attribute that must be present in applets involved in scripting.
            My friendly advice is: don't waste time trying to call script functions from Applets: it's too complex to make the system work in a cross-browser fashion, and usually there's a better way to do the same things.

            Giorgio Maone
            • 3. Re: Applet javascript communication.
              Hi. Thanks for your reply!
              Aoubt how I invoke the javascript functions... - this is what I have tried so far:

              For Netscape, I could simply use the showDocument() function with a "javascript:"- url.
                 public void callJavaScript(String strJSCode)
                    {// does not work on IE:
                          new URL("javascript:"+strJSCode));
                    catch(Exception ex)
              But this does not work on IE.

              Annother aproach I have tried is to use the the JSObject class:
                 public void callJavaScript(String strJSCode)
                    JSObject window = JSObject.getWindow(this); // this=applet
                    Object o = window.eval(strJSCode);
                    System.out.println("o:" +o);
              This works fine on both Netscape6 and IE6, but when the doSomething function in the javascript calls the applet, I get an exception in Netscape.

              Exception occurred during event dispatching:

                   at java.lang.reflect.Constructor.newInstance(Native Method)
                   at sun.plugin.liveconnect.PrivilegedConstructObjectAction.run(Unknown Source)
                   at java.security.AccessController.doPrivileged(Native Method)
                   at sun.plugin.liveconnect.SecureInvocation.ConstructObject(Unknown Source)

              If I use the showDocument("javascript:...")- aproach, I do not get the exception, but still something is wrong because the java method never gets called.

              I have also tried to forget about the callback and just pass everything I need to the applet as a returnvalue from the javascript function, but the returnvalue is always "undefined" when it reaches the applet. It seems that the eval method returns immediatly, before the javascript is executed.

              Javascript- java communication is needed because the applet has some security limitations that does not apply to javascript. Cookie- access is the most important one...
              Ragnvald Barth
              Software enigneer
              • 4. Re: Applet javascript communication.
                Goto mozilla.org in the 0.9.4 release notes under Java they talk about Liveconnect not being fully implemented. They state that it will not be fixed until JDK 1.4 is released.

                I am having the same problems in Netscape 6 and mozilla but works fine in netscape 4.7 and IE 5.X and 6

                The mozilla browser will actually lock when I call

                JSObject jsWin = JSObject.getWindow(this);

                in the applet's public init() method
                • 5. Re: Applet javascript communication.
                  I would recommend to, instead of calling JavaScript from Java, only call Java from JavaScript. You won't need to add browser-specific classes to the applet and the calling scheme is the same on all the browsers. As you invoke Java methods you can pass parameters and return values.

                  I use this approach and it works fine. The applet would be probably smaller, that is Also a good thing.