代码之家  ›  专栏  ›  技术社区  ›  Pedro d'Aquino

如何等待Java小程序在Safari上完成加载?

  •  6
  • Pedro d'Aquino  · 技术社区  · 17 年前

    <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中加载页面时,它会在小程序完成加载之前显示警报(因此会显示消息框) undefined ) . 如果刷新页面,则小程序已加载,并显示警报 function myMethod() { [native code] }

    <body onLoad> .

    <body onAppletLoad="doSomething()"> . 我如何解决这个问题?

    3 回复  |  直到 6 年前
        1
  •  8
  •   Yakk - Adam Nevraumont    13 年前

    我使用一个计时器,它会重置并在放弃之前不断检查多次。

    <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>               
    

        2
  •  3
  •   Chris Chubb    15 年前

    下面是我编写的一个通用函数:

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

    WaitForAppletLoad("fileapplet", 10, 2000, function () {
        document.getElementById("fileapplet").getDirectoriesObject("c:/");
    }, function () {
        alert("Sorry, unable to load the local file browser.");
    });
    
        3
  •  2
  •   Bruno    16 年前

    不久前,我遇到了一个类似的问题,将MAYSCRIPT添加到applet标记解决了我的问题。

    http://www.htmlcodetutorial.com/applets/_APPLET_MAYSCRIPT.html

    希望有帮助!