简体   繁体   English

如何等待Java applet在Safari上完成加载?

[英]How to wait for a Java applet to finish loading on Safari?

This doesn't work in Safari: 这在Safari中不起作用:

<html>
<body>
<applet id="MyApplet" code="MyAppletClass" archive="MyApplet.jar">
<script type="text/javascript">
   alert(document.getElementById('MyApplet').myMethod);
</script>
</body>
</html>

myMethod is a public method declared in MyAppletClass . myMethod是在MyAppletClass声明的公共方法。

When I first load the page in Safari, it shows the alert before the applet has finished loading (so the message box displays undefined ) . 当我第一次在Safari中加载页面时,它会在applet加载完成之前显示警报(因此消息框显示为undefined )。 If I refresh the page, the applet has already been loaded and the alert displays function myMethod() { [native code] } , as you'd expect. 如果我刷新页面,则applet已经加载,并且警报会显示function myMethod() { [native code] } ,正如您所期望的那样。

Of course, this means that the applet methods are not available until it has loaded, but Safari isn't blocking the JavaScript from running. 当然,这意味着applet方法在加载之前不可用,但Safari并没有阻止JavaScript运行。 The same problem happens with <body onLoad> . <body onLoad>同样的问题。

What I need is something like <body onAppletLoad="doSomething()"> . 我需要的是像<body onAppletLoad="doSomething()"> How do I work around this issue? 我该如何解决这个问题?

Thanks 谢谢

PS: I'm not sure if it's relevant, but the JAR is signed. PS:我不确定它是否相关,但JAR已签署。

I use a timer that resets and keeps checking a number of times before it gives up. 我使用一个计时器,在它放弃之前重置并保持多次检查。

<script language="text/javascript" defer>

function performAppletCode(count) {
    var applet = document.getElementById('MyApplet');

    if (!applet.myMethod && count > 0) {
       setTimeout( function() { performAppletCode( --count ); }, 2000 );
    }
    else if (applet.myMethod) {
       // use the applet for something
    }
    else {
       alert( 'applet failed to load' );
    }
}  

performAppletCode( 10 );

</script>               

Note that this assumes that the applet will run in Safari. 请注意,这假定applet将在Safari中运行。 I've had some instances where an applet required Java 6 that simply hangs Safari even with code similar to the above. 我有一些实例,其中一个applet需要Java 6,即使使用与上面类似的代码,也只需挂起Safari。 I chose to do browser detection on the server and redirect the user to an error page when the browser doesn't support the applet. 我选择在服务器上进行浏览器检测,并在浏览器不支持applet时将用户重定向到错误页面。

Here is a generic function I wrote to do just this: 这是我写的一个通用函数:

/* Attempt to load the applet up to "X" times with a delay. If it succeeds, then execute the callback function. */
function WaitForAppletLoad(applet_id, attempts, delay, onSuccessCallback, onFailCallback) {
    //Test
    var to = typeof (document.getElementById(applet_id));
    if (to == "function") {
        onSuccessCallback(); //Go do it.
        return true;
    } else {
        if (attempts == 0) {
            onFailCallback();
            return false;
        } else {
            //Put it back in the hopper.
            setTimeout(function () {
                WaitForAppletLoad(applet_id, --attempts, delay, onSuccessCallback, onFailCallback);
            }, delay);
        }
    }
}

Call it like this: 像这样称呼它:

WaitForAppletLoad("fileapplet", 10, 2000, function () {
    document.getElementById("fileapplet").getDirectoriesObject("c:/");
}, function () {
    alert("Sorry, unable to load the local file browser.");
});

I had a similar problem some time ago and adding MAYSCRIPT to the applet tag solved my problem. 我前段时间遇到了类似的问题,将MAYSCRIPT添加到applet标签解决了我的问题。

Take a peek at this page: http://www.htmlcodetutorial.com/applets/_APPLET_MAYSCRIPT.html 看一下这个页面: http//www.htmlcodetutorial.com/applets/_APPLET_MAYSCRIPT.html

Hope it helps! 希望能帮助到你!

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

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