How to wait for a Java applet to finish loading on Safari?
Solution 1
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. 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.
Solution 2
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.");
});
Solution 3
I had a similar problem some time ago and adding MAYSCRIPT to the applet tag solved my problem.
Take a peek at this page: http://www.htmlcodetutorial.com/applets/_APPLET_MAYSCRIPT.html
Hope it helps!
Pedro d'Aquino
Updated on June 14, 2022Comments
-
Pedro d'Aquino almost 2 years
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
is a public method declared inMyAppletClass
.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 displaysfunction myMethod() { [native code] }
, as you'd expect.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>
.What I need is something like
<body onAppletLoad="doSomething()">
. How do I work around this issue?PS: I'm not sure if it's relevant, but the JAR is signed.
-
Pedro d'Aquino over 14 yearsThank you. It's a shame there isn't a cleaner way of doing this!
-
edoloughlin over 14 yearsI don't see how this is relevant to waiting for the applet to load. Also, the example on the page doesn't work in Firefox 3.5.4 on Snow Leopard.
-
MarioVilas over 11 yearsYou could also try document.getElementById(applet_id).isActive to test if the applet is loaded in the above example, it worked for me. :)
-
Chris Chubb over 11 yearsBut if you do that, document.getElementById(applet_id) will return null when it doesn't exist, and then it will throw a null reference exception of ".isActive". I didn't want to have to test by exception, that is very, very slow in JS.
-
Auri Rahimzadeh almost 11 yearsYou can also simply check for applet. At least, you can in Firefox. I haven't checked in Chrome & IE. That way, you don't have to modify the applet to have a particular method.
-
Andrew Thompson almost 11 yearsAFAIU that only applies to IE. In other words, not OS X & not Safari.
-
targumon about 10 yearsAs far as my tests go,
typeof (document.getElementById(applet_id))
always returnobject
. Did you meantypeof (document.getElementById(applet_id).someFunction)
? -
Chris Chubb about 10 yearsI think that depends on what your applet is and how it's configured. You may have to test both the element and the function. You can't just test typeof (document.getElementById(applet_id).someFunction) by it's self, as document.getElementById(applet_id) may return null early in the life cycle, throwing an invalid object exception when trying to access .someFunction.