加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-14 23:44:39 所属栏目:Java 来源:网络整理
导读:这在Safari中不起作用: htmlbodyapplet id="MyApplet" code="MyAppletClass" archive="MyApplet.jar"script type="text/javascript" alert(document.getElementById('MyApplet').myMethod);/script/body/html myMethod是在MyAppletClass中声明的公共方法.
这在Safari中不起作用:
<html>
<body>
<applet id="MyApplet" code="MyAppletClass" archive="MyApplet.jar">
<script type="text/javascript">
   alert(document.getElementById('MyApplet').myMethod);
</script>
</body>
</html>

myMethod是在MyAppletClass中声明的公共方法.

当我第一次在Safari中加载页面时,它会在applet加载完成之前显示警报(因此消息框显示为undefined).如果我刷新页面,则applet已经加载,并且警报会显示myMethod(){[native code]}函数,正如您所期望的那样.

当然,这意味着applet方法在加载之前不可用,但Safari并没有阻止JavaScript运行. < body onLoad>也会出现同样的问题.

我需要的是像< body onAppletLoad =“doSomething()”>.我该如何解决这个问题?

谢谢

PS:我不确定它是否相关,但JAR已签署.

解决方法

我使用一个计时器,在它放弃之前重置并保持多次检查.
<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>

请注意,这假定applet将在Safari中运行.我有一些实例,其中一个applet需要Java 6,即使使用与上面类似的代码,也只需挂起Safari.我选择在服务器上进行浏览器检测,并在浏览器不支持applet时将用户重定向到错误页面.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读