从小程序调用 javascript 方法时出错
我正在尝试使用 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
is NOT null
。有人遇到过这种情况吗? 多谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我做了一个快速测试,我的发现与 musicfreak 的评论非常相关。
使用以下 Applet:
和以下标记(注意错误的 JavaScript - 我传递的
bar
参数中没有baz
方法):我收到错误:
如果我例如,通过用
alert(bar)
替换bar.baz()
来修复我的 JavaScript 函数,一切正常。长话短说 - 再看看您尝试调用的 JavaScript 函数,以及您的 Applet 在调用时传递的参数。
I did a quick test and my findings correlate well with musicfreak's comment.
With the following Applet:
and the following markup (notice the erroneous JavaScript - there's no
baz
method in thebar
argument I'm passing):I get the error:
If I fix my JavaScript function by replacing
bar.baz()
withalert(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.
在运行与 JavaScript 交互的小程序之前,还有一个进一步的要求。作为安全预防措施,小程序不允许使用 JavaScript,除非网页作者(可能与小程序作者不同)明确授予小程序这样做的权限。要授予此权限,您必须在 HTML 文件中小程序的标记中包含新的 MAYSCRIPT 属性。
例 19.5 显示了一个使用 JavaScript 显示警报对话框的小程序片段。成功编译此小程序后,您可以将其包含在 HTML 文件中,其 HTML 代码如下所示:
如果您忘记包含 MAYSCRIPT 标记,则小程序将不允许与 JavaScript 交互。
http://docstore.mik.ua/orelly/web/jscript/ch19_06.html
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