Oracle provide a JavaScript script called deployJava.js whose job is to detect the browser and version(s) of Java installed and output the correct HTML (APPLET or OBJECT tags) to launch the applet.
Given the variety of operating systems and browsers and Java versions the script has to cope with it does a fair job but there are a number of problems which I'll now describe along with possible workarounds.
The deployJava.js script is located here:
http://www.java.com/js/deployJava.js
You can include it in your HTML by referring to it using the script tag's src attribute:
<script type="text/javascript" src="http://www.java.com/js/deployJava.js"></script>
There is an unsquashed version of the script here but this does not appear to be the same code as the .js script:
http://www.java.com/js/deployJava.txt
If you want to beautify the .js version I recommend using jsbeautifier.org
I strongly recommend downloading the deployJava.js script and serving a local copy on your webserver otherwise your applet may fail if Oracle change the script or their website goes down.

Problems with deployJava.js

1) If no Java is installed, the script will open a popup and install the latest JRE. All the time this is happening, deployJava.js refreshes the webpage containing the applet every second. This looks terrible and given that the JRE download and install will take 2-3 minutes that's a lot of refreshing. Finally on Windows XP, once Java is installed the webpage fails to refresh the final time leaving a grey box with "A Java plugin is required" on FireFox and Chrome and a white box with a red X on IE8. This problem has been reported here: https://forums.oracle.com/forums/thread.jspa?threadID=2486423&stqc=true
A solution is to detect when Java is not installed or below the required version and redirect to the Java download page instead of allowing the popup installer:
<script type="text/javascript" src="http://www.java.com/js/deployJava.js"></script>
<script type="text/javascript">
if (deployJava.versionCheck("1.6.0_31+") == false) {
   userInput = confirm("You need the latest Java(TM) Runtime Environment. Would you like to update now?");
   if (userInput == true) {
       window.location = "http://java.com/";
   }
}
else
{
   var attributes = {id:"applet", name:"TheApplet", code:"TheApplet"};
   var parameters = {jnlp_href: "http://localhost/TheApplet.jnlp"};
   deployJava.runApplet(attributes, parameters, "1.6.0_31");
}
</script>
2) With the Safari browser deployJava.js calls the function
testUsingPluginsArray(jreVersion)
with a number of JRE versions to check if it is present. There is a bug in this function in shown below:
testUsingPluginsArray: function (o)
{
   if ((!navigator.plugins) || (!navigator.plugins.length))
   {
       return false
   }
   var n = navigator.platform.toLowerCase();
   for (var p = 0; p < navigator.plugins.length; ++p)
   {
       s = navigator.plugins[p].description;
       
       if (s.search(/^Java Switchable Plug-in (Cocoa)/) != -1)
       {
           if (this.compareVersions("1.5.0", o))
           {
               return true
           }
       }
       else
       {
           if (s.search(/^Java/) != -1)
           {
               if (n.indexOf("win") != -1)
               {
                   if (this.compareVersions("1.5.0", o) || this.compareVersions("1.6.0", o))
                   {
                       return true
                   }
               }
           }
       }
   }
   if (this.compareVersions("1.5.0", o)) { // ERROR - RETURNS TRUE FOR *ANY* PLUGIN REPORTING VERSION "1.5.0"
       return true
   }
   return false
}
I have submitted a bug report to Oracle.
Steps to reproduce:
Execute this JavaScript in Safari when no JRE is installed and it will report Java is found if you have *any* other plugin reporting version "1.5.0".
On my PC it was Google Update plugin reporting version "1.5.0"
<script language="javascript" type="text/javascript" src="http://www.java.com/js/deployJava.js"></script>
<script language="javascript" type="text/javascript">
alert('Detecting Java');
if (deployJava.versionCheck("1.5+") == false)
{
   alert('No Java or wrong version');
}
else
{
   alert('Java found');
}