简体   繁体   English

在js中调用时,java applet requestFocus()方法错误

[英]java applet requestFocus() method error when called in js

I have error with calling requestFocus() method in JS. 我在JS中调用requestFocus()方法时出错。

Uncaught TypeError: Object #<HTMLAppletElement> has no method 'requestFocus'

This is my JS code between <head> tags 这是我<head>标记之间的JS代码

function onLoad() {
    var attributes = {
        'id': 'Client',
        'archive': 'client.jar',
        'code': 'org.kuzy.client.ui.Applet.class',
        'width': '100%',
        'height': '100%'
    };

    var parameters = {
        'scriptable': 'true'
    };

    deployJava.runApplet(attributes, parameters);

    window.onfocus = function() {
        document.getElementById("Client").requestFocus();
    };
}

There is one strange feature, when I run the code in debug in chrome, everything works fine. 有一个奇怪的功能,当我在chrome中的debug中运行代码时,一切正常。 I hope for your advice. 希望您的指教。

I'm not sure, but probably when you invokes the method requestFocus() applet is not already loaded. 我不确定,但是可能在您调用方法requestFocus()小程序时尚未加载。 Probably when you are debugging you are stopping js execution and you are giving enough time to the applet to be loaded. 可能在调试时,您正在停止js执行,并且给了要加载的applet足够的时间。 So probably you need a callback or some mechanism in order to know when applet is ready. 因此,可能需要回调或某种机制才能知道applet何时准备就绪。

This entire method has a 'bad code' smell to it. 整个方法都有“不良代码”的味道。

  1. The deployJava.runApplet(attributes, parameters); deployJava.runApplet(attributes, parameters); call should be in the page body, and called as soon as the page is written. 调用应该在页面正文中,并在页面写入后立即调用。
  2. The function onLoad() though, should logically be defined in the document head. function onLoad()应该在逻辑上在文档头中定义。
  3. By the time of the onLoad call, the applet should at least be loading , and might even be loaded ready for use. onLoad调用时,applet至少应处于loading状态 ,甚至可以加载以供使用。 Note that the onload event is fired when the page, the scripts, styles and images are downloaded. 请注意,下载页面,脚本,样式和图像时会触发onload事件。 But it does not cover downloads that are handled by the JVM (eg the Jars of the applet or its media resources). 不过,这并不包括那些由JVM处理下载(如applet的jar文件或者其媒体资源)。

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

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