How to wait for a Java applet to finish loading on Safari?

前端 未结 3 1242
温柔的废话
温柔的废话 2021-01-12 15:38

This doesn\'t work in Safari:






        
                      
相关标签:
3条回答
  • 2021-01-12 15:58

    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.");
    });
    
    0 讨论(0)
  • 2021-01-12 16:11

    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!

    0 讨论(0)
  • 2021-01-12 16:14

    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.

    0 讨论(0)
提交回复
热议问题