3
votes

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.

2
I don't know the answer, but my guess would be that "JavaScript error" implies that it's an error in the JavaScript code. Could you post that? - Sasha Chedygov
guess you right. I thought it is an bug and wanna see if any one fix it. <code> function callFromJava(comId) { var textBox = document.getElementById(comId); textBox.setValue("Triggered By Applet"); } </code> I am trying to get the af:inputText by its id. - Khue Vu
What kind of an element is "textBox"? If it's an <input> or <textarea>, try setting the content with textBox.value("Triggered By Applet"); By default, they don't have a "setValue" method. - Lauri Lehtinen
It is an af:inputText. I am wrong on this as I treated it like html component. - Khue Vu
As in adf, I need to use AdfPage.page.findComponentByAbsoluteId("") instead. - Khue Vu

2 Answers

2
votes

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.

-1
votes

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