Thursday, July 31, 2008

Multiple applet instances problem solved (?)

We have had some problems with our applet getting instanciated several times. Seems like it is not only started twice but there are also several instances created by the browser. We have spend quite a lot of time on troubleshooting this and trying to find a solution, and now it seems like we have found a solution:

We changed the APPLET tag in our HTML page to a OBJECT tag. This has to be made specifically for Microsoft Internet Explorer, since it is not supported in other browsers like Firefox and Safari. So in our HTML file we check which browser the user has, and use the OBJECT tag if it is Internet Explorer, the APPLET tag otherwise, something like this:

var html = "<applet archive='MyJar.jar' code='MyApplet.class' height='700' width='1000'>";
if (navigator.appName == 'Microsoft Internet Explorer'){
// Use object tag for MS Internet Explorer
html = "<object classid='clsid:8AD9C840-044E-11D1-B3E9-00805F499D93'" +
" width='1000' height='700'>";
html += "<param name='code' value='MyApplet.class'/>";
html += "<param name='archive' value='MyJar.jar'/>";
}

It seems like this helps, but we do need more testing. Possibly we need to change to using the EMBED tag for other browsers but for now we stick to the APPLET tag.

There is a discussion on this in the Java developer forums here.

No comments: