↑
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
archive
attribute:
<object type=”application/x-java-applet”
archive=”ExampleArchive.jar”
code=”ExampleApplet.class” width=”100” height=”100” id=”ExampleApplet”>
</object>
To support Netscape Navigator 4.x, you use the original
<applet/>
element. To ensure that the code
works in all browsers, you can embed
<applet/>
inside of
<object/>
(similar to the way you can
place
<embed/>
inside of
<object/>
). However, doing so requires the use of the IE-proprietary
<comment/>
element. Although Mozilla and other browsers ignore content inside of
<object/>
, IE
doesn’t, and could end up rendering two copies of the same applet. Inserting the
<comment/>
element
tells IE to ignore the extra content:
<object type=”application/x-java-applet”
code=”ExampleApplet.class” width=”100” height=”100” id=”ExampleApplet”>
<comment>
<applet code=”ExampleApplet.class” width=”100” height=”100”
name=”ExampleApplet”></applet>
</comment>
</object>
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
document.applets
collection, which indexed all
<applet/>
elements by their name attribute and position in the document (similar to
document.forms
and
document.frames
). For example, to get a reference to an applet with the
name
attribute set to
“ExampleApplet”
, you could do the following:
var oApplet = document.applets[“ExampleApplet”];
However, if you use the
<object/>
element to embed applets, the
document.applets
collection doesn’t
include it. When using
<object/>
, you can access the applet using
document.getElementById()
:
var oApplet = document.getElementById(“ExampleApplet”);
If you are using both
<object/>
and
<applet/>
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];
}
544
Chapter 18
21_579088 ch18.qxd 3/28/05 11:43 AM Page 544
Free JavaScript Editor
Ajax Editor
©
→