Skip to content Skip to sidebar Skip to footer

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 tag. Is it possible to add a javascript event that is triggered after the applet is fully loaded? This is

Solution 1:

javascript invoking is rather simple:

Your init() method can include the jsObject declaration and javascript invoking:

@Overridepublicvoidinit() {
// some codeJSObject jsObject = JSObject.getWindow(this);
  jsObject.eval("your javascript");

}

Solution 2:

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. */functionWaitForAppletLoad(applet_id, attempts, delay, onSuccessCallback, onFailCallback) {
    //Testvar to = typeof (document.getElementById(applet_id));
    if (to == 'function' || to == 'object') {
        onSuccessCallback(); //Go do it.returntrue;
    } else {
        if (attempts == 0) {
            onFailCallback();
            returnfalse;
        } 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.");
    });

Solution 3:

You have an initializer function (i think it is run) in java applet. From there you can call a javascript in the web page after initialization work. To work you must add the MAYSCRIPT attribute to your applet definition

<applet id="someId" code="JavaApplet.class" codebase="/foo" archive="Applet.jar" MAYSCRIPT>
</applet>

Code example to invoke a JavaScript:

publicStringinvokeJavaScript(Object caller, String cmd) throws TiNT4Exception {
    printDebug(2, "Start JavaScript >>" + cmd + "<<");
    try {
      // declare variablesMethod getw = null;
      Methodeval = null;
      Object jswin = null;

      // create new instance of class netscape.javascript.JSObjectClass c = Class.forName("netscape.javascript.JSObject"); // , true, this.getClass().getClassLoader()); // does it in IE too// evaluate methodsMethod ms[] = c.getMethods();
      for (int i = 0; i < ms.length; i ++) {
        if (ms[i].getName().compareTo("getWindow") == 0) { getw = ms[i]; }
        elseif (ms[i].getName().compareTo("eval") == 0) { eval = ms[i]; }
      } // for every methodprintDebug(3, "start invokings");
      Object a[] = newObject[1];
      a[0] = caller;
      jswin = getw.invoke(c, a);
      a[0] = cmd;
      Object result = eval.invoke(jswin, a);

      if (result == null) {
        printDebug(3, "no return value from invokeJavaScript");
        return"";
      }

      if (result instanceofString) {
        return (String)result;
      } else {
        return result.toString();
      }
    } catch (InvocationTargetException ite) {
      thrownewTiNT4Exception(ite.getTargetException() + "");
    } catch (Exception e) {
      thrownewTiNT4Exception(e + "");
    }
  } // invokeJavaScript

Solution 4:

You can use the param tag to pass the name of a JS function into your applet:

<appletid="myapplet"code="JavaApplet.class"codebase="/foo"archive="Applet.jar"MAYSCRIPT><paramname="applet_ready_callback"value="myJSfunction"/></applet>

In your applet, get the value of the param and call the function when ready:

@Overridepublicvoidinit() {
  String jsCallbackName = getParameter("applet_ready_callback");
  JSObject jsObject = JSObject.getWindow(this);
  jsObject.eval(jsCallbackName + "()");
}

Solution 5:

I used another way to call a JavaScript function from an applet.

try {
    getAppletContext().showDocument(new URL("javascript:appletLoaded()"));
} catch (MalformedURLException e) {
    System.err.println("Failed to call JavaScript function appletLoaded()");
}

...must be called in the applet class which extends Applet or JApplet. I called the JavaScript function at the end of my start() method.

Post a Comment for "Is It Possible To Register A Javascript Event That Triggers When Java Applet Is Fully Loaded?"