The applet is my own, calling ready() simply returns "yes".
First I tried embedding the applet like this:
<object id="appletIe"
classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
width="100%" height="100%"
codebase="MyApplet.jar">
<param name="archive" value="MyApplet.jar" />
<param name="code" value="MyPackage.Myclass" />
<param name="myParam" value="My Param Value" />
<embed id="applet" MAYSCRIPT=true
type="application/x-java-applet;version=1.6"
width="1px" height="1px"
archive="MyApplet.jar"
code="MyPackage.Myclass"
pluginspage="http://java.com/download/">
</object>
I tried to check it was loaded with javascript by calling ready() on document.ready like this: but I immediately got an error (TypeError: $(...).get(...).ready is not a function) so I assume it tried to call the applet ready() function before it loaded.
$(function(){
if (CheckApplet() == false) {
$('#appletStatus').html('Failed to load applet.');
}
});
function CheckApplet() {
return $('#applet').get(0).ready() == 'yes';
}
Then I tried loading the applet with jquery like this:
This worked a little better, it did not call the applet ready() function until the applet had loaded. But once in a while it doesn't work, the javascript becomes unresponsive, no error is produced even though the applet seems to be loaded ok.
$(function(){
var html = '';
if ($.browser.msie) {
html += '<object id="applet" ';
html += 'classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" ';
html += 'width="100%" height="100%" ';
html += 'codebase="MyApplet.jar"> ';
html += '<param name="archive" value="MyApplet.jar" /> ';
html += '<param name="code" value="MyPackage.Myclass" /> ';
html += '<param name="myParam" value="My Param Value" /> ';
html += '< /object>';
} else {
html += '<embed id="applet"';
html += 'type="application/x-java-applet;version=1.6"';
html += 'width="1px" height="1px" ';
html += 'archive="MyApplet.jar"';
html += 'code="MyPackage.Myclass" ';
html += 'pluginspage="http://java.com/download/"';
html += 'myParam="My Param Value" />';
html += '</embed>';
}
$('#myDiv').append(html);
if (CheckApplet() == false) {
$('#appletStatus').html('Failed to load applet.');
}
});
I'm looking for suggestions on how I can improve this, or other ways to achieve the solution.
As you newer know when your applet comes alive (the JVM must be run, the .jar downloaded and run and .ready() must be connected to the browser), it would be a better strategy to let the applet tell the browser when it is ready. It may do that by invoking a JS function named applet_ready() for example.
See here how an applet can invoke JavaScript functions: http://docs.oracle.com/javase/tutorial/deployment/applet/invokingJavaScriptFromApplet.html
I liked dronus's answer but didn't find Oracle's link to be very helpful so here is my full code, which took me a while to sort out (yep, I know, I'm a bit slow).
At the end of the applet class's init() method ('myApplet' in the example):
JSObject window = JSObject.getWindow(this);
Runnable r = new RunJScript(window);
new Thread(r).start();
In a new separate class in the same package as your applet class:
import netscape.javascript.JSObject;
public class RunJScript implements Runnable
{
private JSObject window;
public RunJScript(JSObject window)
{
this.window = window;
}
public void run()
{
window.call("waitUntilAppletLoaded", null);
}
}
In the web page header which contains your applet:
<SCRIPT language="javascript">
timeOut = 0;
function waitUntilAppletLoaded()
{
if (document.applets['myApplet'].isActive())
{
// Your actions now that the applet has finished loading.
}
else
{
// Increase the timeout by half a second each time round.
timeOut = timeOut + 500
settimeout(waitUntilAppletLoaded(),timeOut)
}
}
</SCRIPT>
If you don't have access to the applet source code, you can poll for the applet to be loaded before calling methods on it.
Have a look to this answer: https://stackoverflow.com/a/7150084/513173
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With