如何从 javascript 调用 WebStart 部署的 applet 函数?

发布于 2024-09-30 03:21:35 字数 1685 浏览 3 评论 0原文

我希望能够使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北方。的韩爷 2024-10-07 03:21:35

我很久以前就知道了,但我忘了回答我自己的问题。所以这里是:

让我们假设您这样定义您的小程序:

<applet id="appletToTest" width=500 height=500 code="com.sample.SampleApplet" archive="JavaApplication6.jar, lib/lib1.jar, lib/lib2.jar">
   <param name="jnlp_href" value="launch.jnlp">
 </applet>

您可以获得一个 javascript!像这样处理小程序:

var parentAPPLET = document.getElementById("appletToTest");

现在,无论您在小程序中使用什么方法,它最好返回字符串或原始值,但即使它不返回,您也可以在假设您知道其方法的情况下对其进行操作。
继续上面的示例,如果在您的 applet 中定义了一个如下所示的函数:

public String returnSomething(){ return "bla"; }

那么您可以使用 javascript 调用它,如下所示:

var parentAPPLET = document.getElementById("appletToTest");
alert (parentAPPLET.returnSomething()); 

恭喜您,您从由 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:

<applet id="appletToTest" width=500 height=500 code="com.sample.SampleApplet" archive="JavaApplication6.jar, lib/lib1.jar, lib/lib2.jar">
   <param name="jnlp_href" value="launch.jnlp">
 </applet>

You can get a javascript! handle to the applet like this:

var parentAPPLET = document.getElementById("appletToTest");

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:

public String returnSomething(){ return "bla"; }

Then you can call it with your javascript like this:

var parentAPPLET = document.getElementById("appletToTest");
alert (parentAPPLET.returnSomething()); 

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.

安穩 2024-10-07 03:21:35

我知道使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文