[英]Error when calling javascript method from applet
我試圖使用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
。 有沒有遇到過這個? 非常感謝。
我做了一個快速測試,我的發現與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 - 我傳遞的bar
參數中沒有baz
方法):
<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隨調用傳遞的參數。
在運行與JavaScript交互的applet之前還有一個要求。 作為安全預防措施,除非網頁作者(可能與applet作者不同)明確授予applet這樣做的權限,否則不允許applet使用JavaScript。 要授予此權限,您必須在HTML文件的applet標記中包含新的MAYSCRIPT屬性。
例19.5顯示了一個使用JavaScript顯示警告對話框的applet片段。 成功編譯此applet后,您可以將其包含在HTML文件中,其中包含以下HTML代碼:
如果您不記得包含MAYSCRIPT標記,則不允許該applet與JavaScript交互。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.