Khue Vu
Khue Vu

Reputation: 3142

Error when calling javascript method from applet

I am trying to call javascript method from an Applet using netscapte.java.JSObject.

in the applet:

JSObject window = JSObject.getWindow(this.Class); 
Object[] args = ....  //arguments 
window.call("javascriptMethodName", args); 

But I get the exception at window.call:

JavaScript error while calling "callFromJava"
netscape.javascript.JSException: JavaScript error while calling "callFromJava"
    at sun.plugin2.main.client.MessagePassingJSObject.newJSException(Unknown Source)
    at sun.plugin2.main.client.MessagePassingJSObject.waitForReply(Unknown Source)
    at sun.plugin2.main.client.MessagePassingJSObject.call(Unknown Source)
    at TextBoxApplet.jButton1_actionPerformed(TextBoxApplet.java:57)
    at TextBoxApplet.access$000(TextBoxApplet.java:16)
    at TextBoxApplet$1.actionPerformed(TextBoxApplet.java:36)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

The JSObject is NOT null. Have anyone encountered this ? Thanks a lot.

Upvotes: 2

Views: 8565

Answers (2)

FabSommavilla
FabSommavilla

Reputation: 157

There is one further requirement before you can run an applet that interacts with JavaScript. As a security precaution, applets are not allowed to use JavaScript unless the web page author (who may be different than the applet author) explicitly gives the applet permission to do so. To give this permission, you must include the new MAYSCRIPT attribute in an applet's tag in the HTML file.

Example 19.5 showed a fragment of an applet that used JavaScript to display an alert dialog box. Once you have successfully compiled this applet, you might include it in an HTML file with HTML code like the following:

If you do not remember to include the MAYSCRIPT tag, the applet will not be allowed to interact with JavaScript.

http://docstore.mik.ua/orelly/web/jscript/ch19_06.html

Upvotes: -1

Lauri Lehtinen
Lauri Lehtinen

Reputation: 10857

I did a quick test and my findings correlate well with musicfreak's comment.

With the following Applet:

public class MyClass extends JApplet {
    public void init() {
        JSObject window = JSObject.getWindow(this); 
        Object[] args = new String[] { "bar" }; 
        window.call("foo", args); 
    }
}

and the following markup (notice the erroneous JavaScript - there's no baz method in the bar argument I'm passing):

<html>
  <head>
    <title>MyAppletTest</title>
  </head>
  <body>
    <script type="text/javascript">
      function foo(bar) { bar.baz(); }
    </script>
    <applet code="MyApplet.class"></applet>
  </body>
</html>

I get the error:

netscape.javascript.JSException: JavaScript error while calling "foo"
    at sun.plugin2.main.client.MessagePassingJSObject.newJSException(Unknown Source)
    at sun.plugin2.main.client.MessagePassingJSObject.waitForReply(Unknown Source)
    at sun.plugin2.main.client.MessagePassingJSObject.call(Unknown Source)
    at MyApplet.init(MyApplet.java:13)

If I fix my JavaScript function by replacing bar.baz() with alert(bar) for example, everything works fine.

Long story short - take another look at the JavaScript function you are trying to call, as well as the parameters your Applet passes with the call.

Upvotes: 2

Related Questions