我正在尝试使用netscapte.java.JSObject
从Applet调用javascript方法。
:
JSObject window = JSObject.getWindow(this.Class);
Object[] args = .... //arguments
window.call("javascriptMethodName", args);
但是我在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)
JSObject
不是null
。有没有遇到过这个?
非常感谢。
答案 0 :(得分:2)
我做了一个快速测试,我的发现与musicfreak的评论很吻合。
使用以下Applet:
public class MyClass extends JApplet {
public void init() {
JSObject window = JSObject.getWindow(this);
Object[] args = new String[] { "bar" };
window.call("foo", args);
}
}
和以下标记(注意错误的JavaScript - 我传递的baz
参数中没有bar
方法:
<html>
<head>
<title>MyAppletTest</title>
</head>
<body>
<script type="text/javascript">
function foo(bar) { bar.baz(); }
</script>
<applet code="MyApplet.class"></applet>
</body>
</html>
我收到错误:
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)
如果我通过将bar.baz()
替换为alert(bar)
来修复我的JavaScript函数,那么一切正常。
长话短说 - 再看看你想要调用的JavaScript函数,以及你的Applet通过调用传递的参数。
答案 1 :(得分:-1)
在运行与JavaScript交互的applet之前还有一个要求。作为安全预防措施,除非网页作者(可能与applet作者不同)明确授予applet这样做的权限,否则不允许applet使用JavaScript。要授予此权限,您必须在HTML文件的applet标记中包含新的MAYSCRIPT属性。
示例19.5显示了一个使用JavaScript显示警告对话框的applet片段。成功编译此applet后,您可以将其包含在HTML文件中,其中包含以下HTML代码:
如果您不记得包含MAYSCRIPT标记,则不允许该applet与JavaScript交互。