Using JavaScript with Applets

JavaScript is a very powerful client- and server-side means of embedding program logic into what would otherwise be static HTML pages. A full discussion of JavaScript programming is beyond the scope of this documentation, so the reader is referred to the JavaScript authoring links on the Helpful Web Authoring Links page.

As the names imply, JavaScript and Java get along very well, and are complimentary to one another in many ways. Two particularly helpful JavaScript capabilities that aid applets, including SiteSurfer, are:

  1. The ability to check whether a web browser has a compatible Java implementation.
  2. The ability to launch new windows for the applet.

The first is critical in today's heterogenous web environment. While there are many kinds of browsers, and many more versions of each browser, not all have adequate support for Java-1.1 based applets. If you or your clients use Netscape Communicator 4.0 and higher; or Internet Explorer 4.0 and higher, then fear not, for these do support Java 1.1 adequately. Earlier versions of these programs, like IE 3.0 and Navigator 2.0 and 3.0, do not have Java 1.1 support unless coupled with the Java Plug-in. Consider the following JavaScript code:


<SCRIPT language="javascript">
<!--
function checkVersion()
{
var ver = navigator.appVersion 
var app = navigator.appName

var ok = true

if (ver.indexOf("MSIE"))
{
ok = (ver.indexOf("3.0") < 0)
}
else if (app.indexOf("Netscape"))
{
ok = (ver.indexOf("2.0") < 0) && (ver.indexOf("3.0") < 0)
}
return ok
}
// -->
</SCRIPT>

The above function, checkVersion, checks to see if the web browser is using a back-level version of IE or Navigator, and if so returns a boolean value of false. Otherwise, it returns true.

By coupling the above function with page-writing JavaScript code, it is possible to render an HTML page with a Java applet or some suitable alternative for browsers that do not support the correct version of Java. This leads into the second noted feature of JavaScript, the ability to launch new windows and write HTML to them dynamically. This is important because when a page containing a Java applet is overwritten or closed, the Java applet is usually stopped by the web browser, and so can no longer function. Instead of loading the applet in the browser's main navigation window, it may be more desirable to launch the applet in a separate window of its own. Again, consider the following code:

<SCRIPT language="javascript">
<!--
function loadApplet()
{
if ((window.name == null) || (window.name == "") || (window.name == "main"))
{
    window.name = "surfWindow" + Math.floor(Math.random() * 1000);
}
surfEngine = window.open("","surfEngine_" + window.name,"status=yes,resizable=yes,width=500,height=450")
surfEngine.document.open()
surfEngine.document.bgColor="CCCCCC"
surfEngine.document.write("<HEAD><TITLE>SiteSurfer</TITLE></HEAD><BODY><CENTER>")
surfEngine.document.write("<APPLET WIDTH='100%' HEIGHT='100%' ALT='Get Java and enable your browser to SiteSurf!' CODE='SiteSurfer.class'>")
surfEngine.document.write("<PARAM NAME='ARCHIVE' VALUE='sitesurf.jar'>")
surfEngine.document.write("<PARAM NAME='CABBASE' VALUE='sitesurf.cab'>")
surfEngine.document.write("<PARAM NAME='DISPLAYTYPE' VALUE='EMBEDDED'>")
surfEngine.document.write("<PARAM NAME='BACKGROUND' VALUE='204,204,204'>")
surfEngine.document.write("<PARAM NAME='HELPURL' VALUE='help.htm'>")
surfEngine.document.write("<PARAM NAME='HELPFRAME' VALUE='show'>")
surfEngine.document.write("<PARAM NAME='DOCUMENTFRAME' VALUE='show'>")
surfEngine.document.write("<PARAM NAME='SHOWRESULTSBUTTONS' VALUE='TRUE'>")
surfEngine.document.write("</APPLET></CENTER></BODY>")
surfEngine.document.close()
}
// -->
</SCRIPT>

If the above code is executed, a new browser window with a unique name will be opened, and the SiteSurfer applet tags will be written to that window's HTML display. The web browser will then parse that HTML to execute the SiteSurfer applet. To reiterate, the benefit here is that the SiteSurfer applet "owns" its own browser window, so it will not be overwritten by any other page, and it will not stop until the user physically closes that window.

The functions as stated above are copied verbatim from the script.htm sample HTML page generated by SiteSurfer Builder whenever you build an index for a site. The only missing thing is how to actually put these functions to good use in an HTML web page. All you have to do is add the following lines to the BODY area of the page:


<SCRIPT language="javascript">
if (checkVersion())
{
document.writeln("<CENTER><A HREF='javascript:loadApplet()'><IMG SRC='powered.gif' BORDER='0' ALT='Powered by SiteSurfer'></A>")
document.writeln("<P><HR>Press the image above to launch SiteSurfer!</CENTER>")
}
else
{
window.alert("Get Java and enable your browser to SiteSurf!")
document.writeln("<H2>This page requires a Java 1.1-enabled browser with JavaScript!</H2>")
}
</SCRIPT>

This code first checks the web browser's version. If it is Java 1.1-compliant (return true), the JavaScript adds a link for launching the new SiteSurfer window. If not (return false), it alerts the user that the browser is incompatible, and prints a short warning message into the page.