HTML <applet> Tag

The <applet> tag was used to embed a Java applet into an HTML document. Java applets are small applications written in the Java programming language that were executed within a web browser. However, the <applet> tag and Java applets, in general, are now considered deprecated and are rarely used due to security concerns and the emergence of more modern web technologies.

HTML Applet Tag: Embedding Legacy Java Applets (Deprecated)

Syntax

<applet code="applet.class" archive="applet.jar" width="pixels" height="pixels" alt="alternative text" name="appletName" mayscript="mayscript" codebase="url">
    <!-- Parameters for the applet -->
    <param name="parameterName" value="parameterValue">
     <!-- Fallback content if applet fails-->
</applet>

Attributes

Attribute Value Description
code filename.class Specifies the name of the compiled Java applet file (.class). Required.
archive filename.jar Specifies the URL of a JAR archive containing the applet's .class files and any necessary resources.
width pixels Sets the width of the applet's display area in pixels. Required.
height pixels Sets the height of the applet's display area in pixels. Required.
alt text Specifies alternative text for users who cannot load the applet.
name appletName Assigns a name to the applet instance, allowing other scripts to reference it.
mayscript "mayscript" Allows the applet to access JavaScript code. Deprecated and not recommended due to security concerns.
codebase URL Specifies the base URL or directory path where the applet's .class files are located. If not specified, defaults to the document’s base URL.
object filename.class Alternative way to specify the applet class file.

Example

<applet code="MyApplet.class" width="300" height="200">
  <p>Your browser does not support Java Applets.</p>
</applet>

More Examples

Example with Archive and Parameters:

<applet code="GameApplet.class" archive="GameApplet.jar" width="600" height="400">
  <param name="level" value="3">
  <param name="playername" value="CodeLucky User">
   <p>Your browser does not support Java Applets. Please enable Java or use a different browser</p>
</applet>

This example shows how to include parameters to pass initial values to your applet. It loads the GameApplet.class file which is inside of GameApplet.jar archive and sets initial level and playername values.

Example with codebase:

<applet code="SimpleApplet.class" codebase="/applets/" width="200" height="150">
 <p>Your browser does not support Java Applets.</p>
</applet>

This example explicitly indicates that SimpleApplet.class is located in the /applets/ directory relative to the current HTML document.

Example with Alternative Text:

<applet code="Banner.class" width="500" height="100" alt="Rotating Banner Applet">
  <p>Your browser does not support Java Applets.</p>
</applet>

The alt attribute here provides descriptive alternative text for users who cannot view the applet.

Example with mayscript:

<!--
Caution: Using "mayscript" is a security risk.
-->
<applet code="ScriptApplet.class" width="300" height="200" mayscript="mayscript">
 <p>Your browser does not support Java Applets.</p>
</applet>

The mayscript attribute allows the applet to access javascript code and it is a security risk and should be avoided.

Browser Support

The <applet> tag was supported by most major browsers in the past. However, due to security concerns and the rise of more modern web technologies, support has been deprecated and is mostly removed from modern browsers.

  • Chrome: Support removed.
  • Firefox: Support removed.
  • Safari: Support removed.
  • Edge: Support removed.
  • Opera: Support removed.

Notes and Tips

  • Deprecation: The <applet> tag is deprecated and should not be used in modern web development.
  • Security Concerns: Java applets have been associated with security vulnerabilities, which is a primary reason for their deprecation.
  • Alternatives: Use modern web technologies like HTML5 Canvas, JavaScript, WebGL, or server-side applications for dynamic content rather than Java applets.
  • Fallback: Always provide fallback content within the <applet> tag to inform users if the applet fails to load or execute.
  • Avoid mayscript: Do not use the mayscript attribute as it is a security risk.
  • Legacy Code: Only encounter <applet> in legacy codebases. Consider migrating to newer technologies for maintainability and security.
  • Plugin Dependence: The <applet> tag required a Java browser plugin, which is often disabled in modern browsers.