如何从 javascript 调用 WebStart 部署的 applet 函数?
我希望能够使用 javascript 调用 JNLP 类的函数。假设在我的小程序中,我有一个如下定义的函数: public String returnSomething(){ return "bla"; 这是从javascript
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<!-- ########################## IMPORTANT NOTE ############################ -->
<!-- This preview HTML page will work only with JDK 6 update 10 and higher! -->
<!-- ###################################################################### -->
<html>
<head>
<title>Test page for launching the applet via JNLP</title>
</head>
<body>
<h3>Test page for launching the applet via JNLP</h3>
<script src="http://java.com/js/deployJava.js"></script>
<script>
var attributes = {
code: "winToJnaApi.NewApplet",
archive: "JavaApplication6.jar, lib/jna.jar, lib/platform.jar",
width: 300,
height: 300,
name: "applet",
id: "app"
};
var parameters = {jnlp_href:"launch.jnlp"}; <!-- Applet Parameters -->
var version = "1.5"; <!-- Required Java Version -->
deployJava.runApplet(attributes, parameters, version);
</script>
<input type = "button" value ="click" onClick = "document.write(document.getElementById("app").doSomething())">
<script type="text/javascript">
document.write(document.getElementById("app"));
</script>
</body>
</html>
调用 JNLP 部署的小程序函数的正确方法吗? 因为这个没有给我任何东西。
I would like to be able to call with javascript a function of my JNLP class. Let's assume in my applet I have a function defined like this: public String returnSomething(){ return "bla"; }
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<!-- ########################## IMPORTANT NOTE ############################ -->
<!-- This preview HTML page will work only with JDK 6 update 10 and higher! -->
<!-- ###################################################################### -->
<html>
<head>
<title>Test page for launching the applet via JNLP</title>
</head>
<body>
<h3>Test page for launching the applet via JNLP</h3>
<script src="http://java.com/js/deployJava.js"></script>
<script>
var attributes = {
code: "winToJnaApi.NewApplet",
archive: "JavaApplication6.jar, lib/jna.jar, lib/platform.jar",
width: 300,
height: 300,
name: "applet",
id: "app"
};
var parameters = {jnlp_href:"launch.jnlp"}; <!-- Applet Parameters -->
var version = "1.5"; <!-- Required Java Version -->
deployJava.runApplet(attributes, parameters, version);
</script>
<input type = "button" value ="click" onClick = "document.write(document.getElementById("app").doSomething())">
<script type="text/javascript">
document.write(document.getElementById("app"));
</script>
</body>
</html>
Is this the proper way to call JNLP-deployed applet functions from javascript?
Cause this one doesn't give me anything.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我很久以前就知道了,但我忘了回答我自己的问题。所以这里是:
让我们假设您这样定义您的小程序:
您可以获得一个 javascript!像这样处理小程序:
现在,无论您在小程序中使用什么方法,它最好返回字符串或原始值,但即使它不返回,您也可以在假设您知道其方法的情况下对其进行操作。
继续上面的示例,如果在您的 applet 中定义了一个如下所示的函数:
那么您可以使用 javascript 调用它,如下所示:
恭喜您,您从由 JWS 部署的 javascript 调用了 Java Applet 方法。
还有另一种方法可以做到这一点,但它只能在 Firefox 中使用,所以最好使用这个方法。尽管该技术已被弃用,但 LiveConnect 规则仍然适用。
I got it a long time ago, but I forgot to answer my own question. So here it is:
Let's assume you define your applet like this:
You can get a javascript! handle to the applet like this:
Now, whatever method you have in your applet, it better return String or a primitive value, but even if it doesn't you may operate on it assuming you know its methods.
Continuing my example from above, if in your applet you have a function defined like this:
Then you can call it with your javascript like this:
And congratulations, you called a Java Applet method from javascript that was deployed by JWS.
There is another way of doing that but it would work only in Firefox, so better use this one. LiveConnect rules still apply, even though the technology is deprecated.
我知道使用 JNLP 启动的小程序不再可供 JS 访问,但我可能是错的。为了进一步研究它,我建议删除deployJava.js并对applet元素进行硬编码。
I understand that applets launched using JNLP are no longer accessible to JS, but I might be wrong. To investigate it further, I would recommend removing the deployJava.js and hard coding the applet element.