awe awe - 6 months ago 15
Java Question

Is it possible to register a javascript event that triggers when java applet is fully loaded?

I have a web application that uses java applet defined in a

<applet>
tag. Is it possible to add a javascript event that is triggered after the applet is fully loaded? This is some initialization javascript that is dependent on that the applet is fully loaded and valid.

Answer

If you don't have source code control over the applet, you can poll for the applet to be loaded before calling methods on it. Here is a utility function I wrote that does 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' || to == 'object') {
        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 () {
        BuildTree("c:/");
    }, function () {
        alert("Sorry, unable to load the local file browser.");
    });