<APPLET>NN 2   IE 3   HTML 3.2
<APPLET>...</APPLET>End Tag: Required
 

You can embed an executable chunk of Java code in an HTML document in the form of an applet. An applet occupies a rectangular area of the page, even if it is only one-pixel square. An applet may require that some initial values be set from the HTML document. One or more PARAM elements can be used to pass parameters to the applet before the applet starts running (provided the applet is written to accept these parameters). PARAM elements go between the start and end tags of an APPLET element.

Applets are compiled by their authors into class files (filename suffix .class). An applet class file must be in the same directory as, or a subdirectory of, the HTML document that loads the applet. Key attributes of the APPLET element direct the browser to load a particular class file from the necessary subdirectory.

All user interface design for the applet is programmed into the applet in the Java language. One of the roles of attributes in the APPLET element is to define the size and other geographical properties of the applet for its rendering on the page. Recent browsers allow JavaScript scripts to communicate with the applet, as well as allowing applets to access document elements.

Note that HTML 4.0 deprecates the APPLET element in favor of the more generic OBJECT element. Browser support for the APPLET element will continue for some time to come, however.

 
Example
<APPLET CODE="simpleClock.class" NAME="myClock" WIDTH=400 HEIGHT=50>
<PARAM NAME=bgColor VALUE="black">
<PARAM NAME=fgColor VALUE="yellow">
</APPLET>
 
Object Model Reference
NN [window.]document.applets[i]
[window.]document.appletName
IE [window.]document.applets[i]
[window.]document.appletName
ALIGNNN 2   IE 3   HTML 3.2
ALIGN="alignmentConstant"Optional
 

The ALIGN attribute determines how the rectangle of the applet aligns within the context of surrounding content. See the section "Alignment Constants" earlier in this chapter for description of the possibilities defined in both Navigator and Internet Explorer for this attribute. Only a subset of the allowed constant values is specified in the HTML recommendation. Although Navigator included this attribute in Version 2, only BOTTOM, LEFT, RIGHT, and TOP were implemented in that version.

Both browsers follow the same rules on laying out content surrounding an applet whose ALIGN attribute is set, but the actual results are sometimes difficult to predict when the surrounding content is complex. A thorough testing of rendering possibilities with browser windows set to various sizes prevents surprises later.

 
Example
<APPLET CODE="simpleClock.class" NAME="myClock" ALIGN=ABSMIDDLE 
WIDTH=400 HEIGHT=50></APPLET>
 
Value
Case-insensitive constant value.
 
Default bottom
 
Object Model Reference
IE [window.]document.applets[i].align
[window.]document.appletName.align
ALTNN 3   IE 3   HTML 3.2
ALT="textMessage"Optional
 

If a browser does not have the facilities to load and run Java applets or if the browser has Java support turned off in its preferences, the text assigned to the ALT attribute is supposed to display in the document where the APPLET element's tag appears. Typically, this text provides advice on what the page visitor is missing by not being able to load the Java applet. Unlike the NOSCRIPT or NOFRAMES elements, there is no corresponding element for an absent Java applet capability. In practice, browsers don't necessarily display this message for applets that fail to load for a variety of reasons.

In the event that this feature should work better in the future, use the ALT attribute with care. If the applet is not a critical part of your page's content, you may just want the rest of the page to load without calling attention to the missing applet in less-capable browsers. The alternate message may be more disturbing to the user than a missing applet.

 
Example
<APPLET CODE="simpleClock.class" NAME="myClock" ALIGN=ABSMIDDLE 
ALT="A Java clock applet." WIDTH=400 HEIGHT=50></APPLET>
 
Value
Any quoted string of characters.
 
Default None.
ARCHIVENN 3   IE n/a   HTML 4
ARCHIVE="archiveFileURL"Optional
 

The precise meaning of the ARCHIVE attribute varies between the HTML 4.0 recommendation and Netscape's implementation. The basic idea behind Netscape's ARCHIVE attribute is that an author can package together multiple class files into a single uncompressed .zip archive file and let the browser load the entire set of classes at one time. This can offer a performance improvement over loading just the main class file (specified by the CODE attribute) and then letting the class loader fetch each additional class file as needed.

In addition to specifying the ARCHIVE attribute, be sure to include a CODE attribute that names the main class to load. Navigator first looks for the presence of that class file in the archive. If the file is missing from the archive, Navigator loads the CODE class file separately. (That class may then load additional supporting class files individually.) Navigator requires that the archive file have a .zip filename extension. The URL must also be relative to the CODEBASE location.

The HTML specification allows multiple URLs to be specified (in a space-delimited list) for additional class or other resource files. This design is in anticipation of the same attribute being used with the OBJECT element, which the W3C has deemed to be the successor to the APPLET element.

 
Example
<APPLET CODE="ScriptableClock.class" ARCHIVE="myClock.zip" WIDTH=400 HEIGHT=50>
</APPLET>
 
Value
Case-sensitive URI.
 
Default None.
CODENN 2   IE 3   HTML 3.2
CODE="fileName.class"Required
 

The name of the main class file that starts and runs the applet. If the CODEBASE attribute is not specified, the CODE attribute must include a path from the directory that stores the HTML document loading the applet. You might get away with omitting the .class filename extension, but don't take any chances: be complete with the class name. Most servers are case sensitive, so also match case of the actual class filename.

 
Example
<APPLET CODE="applets/ScriptableClock.class" WIDTH=400 HEIGHT=50>
</APPLET>
 
Value
Case-sensitive .class filename or complete path relative to the HTML document.
 
Default None.
 
Object Model Reference
IE [window.]document.applets[i].code
[window.]document.appletName.code
CODEBASENN 2   IE 3   HTML 3.2
CODEBASE="path"Optional
 

Path to the directory holding the class file designated in either the CODE or ARCHIVE attribute. The CODEBASE attribute does not name the class file, just the path. You can make this attribute a complete URL to the directory, but don't try to access a codebase outside of the domain of the current document: security restrictions may prevent the class from loading. A full path and filename can be set together in the CODE or OBJECT attribute, eliminating the need for the CODEBASE attribute setting.

 
Example
<APPLET CODE="ScriptableClock.class" CODEBASE="applets/" WIDTH=400 HEIGHT=50>
</APPLET>
 
Value
Case-sensitive pathname, usually relative to the directory storing the current HTML document.
 
Default None.
 
Object Model Reference
IE [window.]document.applets[i].codeBase
[window.]document.appletName.codeBase
DATAFLD, DATASRC 
  
 
>
HEIGHT, WIDTHNN 2   IE 3   HTML 3.2
WIDTH="pixels"
HEIGHT="pixels"
Required
 

The size that a Java applet occupies in a document is governed by the HEIGHT and WIDTH attribute settings. Some browser versions might allow you to get away without assigning these attributes, letting the applet's own user interface design determine the height and width of its visible rectangle. As with images, however, it is more efficient for the browser's rendering engine when you explicitly specify the object's dimensions. Make a habit of supplying these values for all applets, as you should for all images or other visible external objects.

 
Example
<APPLET CODE="ScriptableClock.class" WIDTH=400 HEIGHT=50>
</APPLET>
 
Value
Positive integer pixel values (optionally quoted). You cannot entirely hide an applet by setting values to zero, but you can reduce its height and width to one pixel in each dimension. If you want to hide an applet, do so with DHTML by setting its positioning display attribute to none.
 
Default None.
 
Object Model Reference
IE [window.]document.applets[i].height
[window.]document.appletName.height
[window.]document.applets[i].width
[window.]document.appletName.width
HSPACE, VSPACENN 2   IE 3   HTML 3.2
HSPACE="pixels"
VSPACE="pixels"
Optional
 

You can put some empty space ("air") between an applet and any surrounding content by assigning pixel values to the HSPACE and VSPACE attributes. The VSPACE attribute governs space above and below the applet; the HSPACE attribute governs space to the left and right of the applet. For browsers that are style sheet savvy, you are perhaps better served by using the padding and/or margin style attributes to gain control down to individual sides, if you so desire.

 
Example
<APPLET CODE="ScriptableClock.class" WIDTH=400 HEIGHT=50 HSPACE=3 VSPACE=4>
</APPLET>
 
Value
Positive integer pixel values (optionally quoted).
 
Default 0
 
Object Model Reference
IE [window.]document.applets[i].hspace
[window.]document.appletName.hspace
[window.]document.applets[i].vspace
[window.]document.appletName.vspace
IDNN n/a   IE 4   HTML 4
ID="elementIdentifier"Optional
 

A unique identifier that distinguishes this element from all the rest in the document. Can be used to associate a single element with a style rule naming this attribute value as an ID selector. An element can have an ID assigned for uniqueness as well as a class for inclusion within a group. See .

If you assign an ID attribute and not a NAME attribute, the value of the ID attribute can be used as the applet's name in script reference forms that use the element name (document.appletName).

 
Example
<APPLET ID="clocker" CODE="ScriptableClock.class" WIDTH=400 HEIGHT=50>
</APPLET>
 
Value
Case-sensitive identifier.
 
Default None.
 
Object Model Reference
IE [window.]document.applets[i].id
[window.]document.appletName.id
MAYSCRIPTNN 3   IE 4   HTML n/a
MAYSCRIPTOptional
 

Navigator 3 introduced a technology called LiveConnect, which allowed scripts to communicate with Java applets and vice versa. For security reasons, an applet's communications facilities with scripts must be explicitly switched on by the page author. By adding the MAYSCRIPT attribute to the applet's tag, an applet that is written to take advantage of the document objects and scripts can address those items. In other words, the HTML is granting the applet the ability to reach scripts in the document. This attribute is a simple switch: when the attribute name is present, it is turned on.

One more step is required for an applet to communicate with JavaScript. The applet code must import a special Netscape class called JSObject.class. This class file (and its companion exception class) are built into the Java support in the Windows version of Internet Explorer 4. Although the execution is not perfect in IE 4, applets can perform basic communication with scripts.

 
Example
<APPLET CODE="ScriptableClock.class" WIDTH=400 HEIGHT=50 MAYSCRIPT>
</APPLET>
 
Value
No value assigned to the attribute. The presence of the attribute name sets turns on applet-to-script communication.
 
Default Off.
NAMENN 2   IE 3   HTML 3.2
NAME="elementIdentifier"Optional
 

If you are scripting an applet, it is usually more convenient to create a reference to the applet by using a unique name you assign to the applet. Then, if you edit the page and move or delete multiple applets on the page, you do not have to worry about adjusting index values to array-style references. In IE 4, you have the option of omitting the NAME attribute and using the ID attribute value in script references to the applet object.

 
Example
<APPLET NAME="clock2" CODE="ScriptableClock.class" WIDTH=400 HEIGHT=50>
</APPLET>
 
Value
Case-sensitive identifier.
 
Default None.
 
Object Model Reference
IE [window.]document.applets[i].name
[window.]document.appletName.name
SRCNN n/a   IE 4   HTML n/a
SRC="URL"Optional
 

Internet Explorer 4 defines this attribute as the URL for an "associated file." This may be the same as the ARCHIVE attribute defined in HTML and Navigator specifications. The SRC attribute is not a substitute for the CODE and/or CODEBASE attributes.

 
Value
A complete or relative URL.
 
Default None.
 
Object Model Reference
IE [window.]document.applets[i].src
[window.]document.appletName.src
Hosted by uCoz