Main Page

Referencing applets in JavaScript

In this mime type, JPI is short for
Java PlugIn
and it ensures that the applet is not run unless the plugin
on the user ’s computer is exactly equal to version 1.4.2. So even if the user has version 1.4.3, the applet
does not run. For this reason, it’s best to omit the plugin version to avoid any annoyance for your users.
The applet class can be contained within a JAR (Java Archive) file. In that case, specify the JAR file by
using the
<object type=”application/x-java-applet”
code=”ExampleApplet.class” width=”100” height=”100” id=”ExampleApplet”>
To support Netscape Navigator 4.x, you use the original
element. To ensure that the code
works in all browsers, you can embed
inside of
(similar to the way you can
inside of
). However, doing so requires the use of the IE-proprietary
element. Although Mozilla and other browsers ignore content inside of
, IE
doesn’t, and could end up rendering two copies of the same applet. Inserting the
tells IE to ignore the extra content:
<object type=”application/x-java-applet”
code=”ExampleApplet.class” width=”100” height=”100” id=”ExampleApplet”>
<applet code=”ExampleApplet.class” width=”100” height=”100”
Referencing applets in JavaScript
After an applet has been included in an HTML page, you need a way to access it via JavaScript.
Traditionally, applets were referenced through the
collection, which indexed all
elements by their name attribute and position in the document (similar to
). For example, to get a reference to an applet with the
attribute set to
, you could do the following:
var oApplet = document.applets[“ExampleApplet”];
However, if you use the
element to embed applets, the
collection doesn’t
include it. When using
, you can access the applet using
var oApplet = document.getElementById(“ExampleApplet”);
If you are using both
for compatibility with older browsers, you should use
a function to determine the appropriate method:
function getApplet(sName) {
if (document.getElementById) {
return document.getElementById(sName);
} else {
return document.applets[sName];
Chapter 18
21_579088 ch18.qxd 3/28/05 11:43 AM Page 544

JavaScript EditorFree JavaScript Editor     Ajax Editor