问题描述
这在Safari中不起作用:
This doesn't work in 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已经加载,警报会显示 function myMethod(){[native code]}
,正如您所期望的那样。
When I first load the page in Safari, it shows the alert before the applet has finished loading (so the message box displays 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方法在加载之前不可用,但Safari并没有阻止JavaScript运行。 < body onLoad>
也会出现同样的问题。
Of course, this means that the applet methods are not available until it has loaded, but Safari isn't blocking the JavaScript from running. The same problem happens with <body onLoad>
.
我需要的是像< body onAppletLoad =doSomething()>
。我如何解决这个问题?
What I need is something like <body onAppletLoad="doSomething()">
. How do I work around this issue?
谢谢
PS:我不确定它是否相关,但JAR已签名。
PS: I'm not sure if it's relevant, but the JAR is signed.
推荐答案
我使用的计时器会在放弃之前重置并保持多次检查。
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>
请注意,这假定applet将在Safari中运行。我有一些实例,其中一个applet需要Java 6,即使使用类似于上面的代码,它也只是挂起Safari。我选择在服务器上进行浏览器检测,并在浏览器不支持applet时将用户重定向到错误页面。
Note that this assumes that the applet will run in Safari. I've had some instances where an applet required Java 6 that simply hangs Safari even with code similar to the above. 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.
这篇关于如何等待Java applet在Safari上完成加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!