I was looking for a reliable way to detect if the java plugin is available on a browser, i’ve had a look at various javascript methods and sun’s deployJava.js but they all seem a little unreliable especially on browser like safari and konqueror where no information is reported at all from javascript.
I’ve since found the only reliable way is to actually have a java applet launch and report back that it is available.
Thought i’d post the method here in case if it is useful to others.
java code
import java.applet.Applet;
import netscape.javascript.JSObject;
public class Test extends Applet {
JSObject win;
public void init() {
win = JSObject.getWindow(this);
Object[] param = { System.getProperty("java.version") };
// alert javascript that java plugin is found
win.call("javaSupported", param);
}
}
html/javascript code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<body onLoad="checkJava()">
<script type="text/javascript">
var javaFound = false;
function javaSupported(version) {
javaFound = true;
var gamebox=document.getElementById('appletbox');
// remove applet
gamebox.innerHTML = 'Java version ' + version + ' available';
}
function javaNotFound() {
if (!javaFound) {
var appletbox=document.getElementById('appletbox');
appletbox.innerHTML = 'Java Not Found';
}
}
function checkJava() {
// wait 10 seconds if java hasn't responded presume its not found
setTimeout('javaNotFound();', 10000);
}
</script>
<div id="appletbox">Checking if Java Available...<applet code="Test" width="1" height="1" MAYSCRIPT/></div>
</body>
</html>
What the code does is wait 10 seconds for the applet to report back through liveconnect that it is available, if it does not report back it presumes that java is not available.
Another thing that makes this faster than using a full applet is that the innerHTML quickly removes the applet once it has reported back and avoids any further slow downs from the applet that the paint methods, etc bring. You can just change this if you are using a full applet to leave applet there instead of removing it.
example http://kappa.javaunlimited.net/temp/testjava.html
works for me on all the browsers and platforms i’ve tested on IE6,IE7,Opera,Safari,Firefox,Chrome, Konqueror on windows and linux.