How to wait for Java applet to finish loading in Safari?

This does not 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 is a public method declared in MyAppletClass .

When I first load the page in Safari, it shows a warning before the applet finishes loading (so it displays undefined in the message box). If I refresh the page, the applet is already loaded, and the warning displays function myMethod() { [native code] } , as you would expect.

Of course, this means that the methods of the applet are not available until it loads, but Safari does not block the launch of JavaScript. The same problem occurs with <body onLoad> .

I need something like <body onAppletLoad="doSomething()"> . How do I get around this problem?

PS: I'm not sure if this is relevant, but the JAR is signed.

+6
java javascript safari javascript-events dom-events
source share
3 answers

I use a timer that resets and continues to check several times before it refuses.

 <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 be launched in Safari. I had some cases where the applet required Java 6, which just freezes Safari even with code like the one above. I decided to make browser detection on the server and redirect the user to the error page when the browser does not support the applet.

+8
source share

Here is a general function that I wrote to do just that:

 /* 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); } } } 

Name it as follows:

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

I had a similar problem a while ago, and adding MAYSCRIPT to the applet tag solved my problem.

Take a look at this page: http://www.htmlcodetutorial.com/applets/_APPLET_MAYSCRIPT.html

Hope this helps!

+2
source share

All Articles