Plugin detection via JavaScript

Below I’m pasting in some code to determine whether or not the browser has a specified version of the plugin or above.

My intended application is to use this transparently, displaying the ‘real’ applet in the body if the plugin meets the specified requirement and either using document.setLocation or popping up a new window to navigate users to the “Get Java” page if not.

I’m wondering if some of you would mind testing this out in whatever browsers you have handy. I’m not particularly concerned with supporting browsers more than a generation or two ago, but correct execution on most/all current browsers is important IMO.

I have dialup access, so I need to download some other browsers anyway but it will take a while. It does work on IE 6.

detection.html


<HTML>
<HEAD>
<TITLE>Detect Java Plugin</TITLE>
</HEAD>

<SCRIPT LANGUAGE="JavaScript"> 

function checkJavaPlugin() {

      var minimumVersion = "1.4.1";
      var applet = document.applets["DetectPluginApplet"];
      if(applet == null) {
            alert("You do not have support for Java. Please download a plugin.");
      }
      var supported = applet.meetsRequirements(minimumVersion);
      if(supported) {
            document.write("Your browser supports the minimum requirement of\n" + 

minimumVersion);
      }
      else document.write("Your browser does not meet the requirement of\n" + 

minimumVersion + ". At this point a call to document.setLocation() would redirect you to the 

\"Get Java\" page.");
}

</SCRIPT> 

<BODY onLoad="checkJavaPlugin()">
<APPLET NAME="DetectPluginApplet" CODE="DetectPlugin.class" WIDTH=0 HEIGHT=0>
</APPLET>
</BODY>
</HTML>

DetectPlugin.java


public class DetectPlugin extends java.applet.Applet {

      public boolean meetsRequirements(String minimumVersion){
            String version = System.getProperty("java.version");
            return version.compareTo(minimumVersion) >= 0;
      }
}

Also, any tips on improving this (especially if it turns out that it is not cross-browser-compatible) are very welcome!

Isn’t this functionality provided by the Java Plugin HTML converter?

Anyways, one thing to watch out for is that if you compile your DetectPlugin class with the 1.4 javac, it might not run on older 1.1 VM browsers. I encountered this problem before, I think the javac -target option fixes it.

Here are my reasons for not liking the HTML converter, from what I’ve seen of it:

  1. You have to hard-code the URL of a specific JRE binary that you require
  2. The HTML converter program does not know which JREs are available (or what directory they are in) which means you have to dig around and find it yourself
  3. The HTML converter relies on certain browsers not recognizing certain tags. This is prone to breaking your applet when, for example, Netscape starts recognizing the COMMENT tag the same way that IE does.
  4. Multi-browser support (and without using this option, why Java anyway?) requires a gigantic mess of HTML that is tricky to edit later on
  5. Using my method, you can check for the user’s version of Java pretty transparently on any page of your website and direct them however you like – IE you can provide your own customized instructions on what to look for on the “Get Java” page.
  6. Assuming this JavaScript/Java detection works on all or most browsers that can run Java, it is much less likely to break with future versions the browsers, since the JavaScript language is going to be backwards compatible whereas non-support for certain HTML tags is inherently not backwards-compatible
  7. This mechanism is a lot closer to the process for detecting and redirecting users depending on what version of Flash they have installed. The ease of doing that is something that Sun should be imitating by now :wink:

Here is what the HTML converter spit out automatically for the DetectPlugin class:


<!--"CONVERTED_APPLET"-->
<!-- CONVERTER VERSION 1.0 -->
<SCRIPT LANGUAGE="JavaScript"><!--
    var _info = navigator.userAgent; var _ns = false;
    var _ie = (_info.indexOf("MSIE") > 0 && _info.indexOf("Win") > 0 && _info.indexOf("Windows 3.1") < 0);
//--></SCRIPT>
<COMMENT><SCRIPT LANGUAGE="JavaScript1.1"><!--
    var _ns = (navigator.appName.indexOf("Netscape") >= 0 && ((_info.indexOf("Win") > 0 && _info.indexOf("Win16") < 0 && java.lang.System.getProperty("os.version").indexOf("3.5") < 0) || (_info.indexOf("Sun") > 0) || (_info.indexOf("Linux") > 0)));
//--></SCRIPT></COMMENT>

<SCRIPT LANGUAGE="JavaScript"><!--
    if (_ie == true) document.writeln('<OBJECT classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" WIDTH = 0 HEIGHT = 0 NAME = "DetectPluginApplet"  codebase="http://java.sun.com/products/plugin/1.1.1/jinstall-111-win32.cab#Version=1,1,1,0"><NOEMBED><XMP>');
    else if (_ns == true) document.writeln('<EMBED type="application/x-java-applet;version=1.1" java_CODE = "DetectPlugin.class" NAME = "DetectPluginApplet" WIDTH = 0 HEIGHT = 0   pluginspage="http://java.sun.com/products/plugin/1.1.1/plugin-install.html"><NOEMBED><XMP>');
//--></SCRIPT>
<APPLET CODE = "DetectPlugin.class" WIDTH = 0 HEIGHT = 0 NAME = "DetectPluginApplet" ></XMP>
<PARAM NAME = CODE VALUE = "DetectPlugin.class" >
<PARAM NAME = NAME VALUE = "DetectPluginApplet" >

<PARAM NAME="type" VALUE="application/x-java-applet;version=1.1">

</APPLET>

</NOEMBED></EMBED></OBJECT>

compare this to what I want to use:


<APPLET NAME="DetectPluginApplet" CODE="DetectPlugin.class" WIDTH=0 HEIGHT=0> 

At this point, I guess it expects me to look up the cab location for win32 systems, mac systems, *nix systems, etc? The thing is, I don’t even want to mess with all of this crap if I can avoid it ;D

… getting the chilling notion that this is only of interest to myself, but FYI it does work in Opera 6.5

Edit: The information I have right now –

Works on IE6, Netscape 7, and Opera 6.5 with 1.4.1 on Win2k. Works in IE6 with Microsoft’s VM in Win2k.

You may be able to find some good plugin detection scripts on javascript.internet.com