简体   繁体   English

如何测试使用javascript加载java applet?

[英]How can I test that a java applet is loaded using javascript?

The applet is my own, calling ready() simply returns "yes". 小程序是我自己的,调用ready()只返回“是”。

First I tried embedding the applet like this: 首先,我尝试嵌入这样的applet:

        <object id="appletIe"
        classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
        width="100%" height="100%"
        codebase="MyApplet.jar">
        <param name="archive" value="MyApplet.jar" />
        <param name="code" value="MyPackage.Myclass" />
        <param name="myParam" value="My Param Value" />
         <embed id="applet" MAYSCRIPT=true
        type="application/x-java-applet;version=1.6"
        width="1px" height="1px"
        archive="MyApplet.jar"
        code="MyPackage.Myclass"
        pluginspage="http://java.com/download/">
        </object>

I tried to check it was loaded with javascript by calling ready() on document.ready like this: but I immediately got an error (TypeError: $(...).get(...).ready is not a function) so I assume it tried to call the applet ready() function before it loaded. 我试着通过在document.ready上调用ready()来检查它是否已经加载了javascript,但是我立刻得到了一个错误(TypeError:$(...)。get(...)。ready不是函数)所以我假设它在加载之前尝试调用applet ready()函数。

$(function(){
  if (CheckApplet() == false) {
          $('#appletStatus').html('Failed to load applet.');
      } 
  });



  function CheckApplet() {
      return $('#applet').get(0).ready() == 'yes';
  }

Then I tried loading the applet with jquery like this: 然后我尝试用这样的jquery加载applet:

This worked a little better, it did not call the applet ready() function until the applet had loaded. 这样做效果好一点,在applet加载之前它没有调用applet ready()函数。 But once in a while it doesn't work, the javascript becomes unresponsive, no error is produced even though the applet seems to be loaded ok. 但偶尔它不起作用,javascript变得没有响应,即使applet似乎加载好,也不会产生错误。

 $(function(){
  var html = '';

    if ($.browser.msie) {
        html += '<object id="applet" ';
        html += 'classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" ';
        html += 'width="100%" height="100%" ';
        html += 'codebase="MyApplet.jar"> ';
        html += '<param name="archive" value="MyApplet.jar" /> ';
        html += '<param name="code" value="MyPackage.Myclass" /> ';
        html += '<param name="myParam" value="My Param Value" /> ';
        html += '< /object>';
    } else {
        html += '<embed id="applet"';
        html += 'type="application/x-java-applet;version=1.6"';
        html += 'width="1px" height="1px" ';
        html += 'archive="MyApplet.jar"';
        html += 'code="MyPackage.Myclass" ';
        html += 'pluginspage="http://java.com/download/"';
        html += 'myParam="My Param Value" />';
        html += '</embed>';
    }
    $('#myDiv').append(html);

if (CheckApplet() == false) {
          $('#appletStatus').html('Failed to load applet.');
      } 

});

I'm looking for suggestions on how I can improve this, or other ways to achieve the solution. 我正在寻找关于如何改进这个或其他方法来实现解决方案的建议。

As you newer know when your applet comes alive (the JVM must be run, the .jar downloaded and run and .ready() must be connected to the browser), it would be a better strategy to let the applet tell the browser when it is ready. 当你刚知道你的applet活跃时(必须运行JVM,下载并运行.jar和.ready()必须连接到浏览器),让applet告诉浏览器什么时候更好的策略准备好了。 It may do that by invoking a JS function named applet_ready() for example. 它可以通过调用名为applet_ready()的JS函数来实现。

See here how an applet can invoke JavaScript functions: http://docs.oracle.com/javase/tutorial/deployment/applet/invokingJavaScriptFromApplet.html 请参阅此处applet如何调用JavaScript函数: http//docs.oracle.com/javase/tutorial/deployment/applet/invokingJavaScriptFromApplet.html

I liked dronus's answer but didn't find Oracle's link to be very helpful so here is my full code, which took me a while to sort out (yep, I know, I'm a bit slow). 我喜欢dronus的答案,但没有发现Oracle的链接非常有用,所以这里是我的完整代码,这花了我一段时间来整理(是的,我知道,我有点慢)。

At the end of the applet class's init() method ('myApplet' in the example): 在applet类的init()方法结束时(示例中为'myApplet'):

JSObject window = JSObject.getWindow(this);
Runnable r = new RunJScript(window);
new Thread(r).start();

In a new separate class in the same package as your applet class: 在与applet类相同的包中的新单独类中:

import netscape.javascript.JSObject;

public class RunJScript implements Runnable
{
    private JSObject window;
    public RunJScript(JSObject window) 
    {
        this.window = window;
    }

   public void run() 
   {
        window.call("waitUntilAppletLoaded", null);
   }

} }

In the web page header which contains your applet: 在包含您的applet的网页标题中:

<SCRIPT language="javascript">
    timeOut = 0;
    function waitUntilAppletLoaded() 
    {
        if (document.applets['myApplet'].isActive()) 
        {
            // Your actions now that the applet has finished loading.
        }
        else 
        {
            // Increase the timeout by half a second each time round.
            timeOut = timeOut + 500
            settimeout(waitUntilAppletLoaded(),timeOut)
        }
    }
</SCRIPT>

If you don't have access to the applet source code, you can poll for the applet to be loaded before calling methods on it. 如果您无权访问applet源代码,则可以在调用其上的方法之前轮询要加载的applet。

Have a look to this answer: https://stackoverflow.com/a/7150084/513173 看看这个答案: https//stackoverflow.com/a/7150084/513173

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM