HTML Document applets
Collection: Accessing Applet Elements
The document.applets
collection in HTML provides a way to access all <applet>
elements embedded within an HTML document. This collection is a live HTMLCollection
, meaning any changes to the document's applet elements are immediately reflected in the collection. Although the <applet>
tag is deprecated in favor of more modern technologies, understanding how it was used and how to access applets via the DOM can be helpful for maintaining older applications or for historical context. This article delves into using the document.applets
collection with detailed examples.
What is the document.applets
Collection?
The document.applets
collection is a read-only property that returns an HTMLCollection
of all the <applet>
elements within an HTML document. This collection is primarily used in JavaScript to access applet elements, manipulate their properties, or interact with their methods.
Purpose of the document.applets
Collection
The main purpose of this collection is to:
- Provide a dynamic list of all
<applet>
elements in the document. - Allow developers to programmatically access applet elements using JavaScript.
- Enable runtime manipulation of the applets.
- Facilitate the removal or addition of applet elements via JavaScript (though the
applets
collection itself is read-only, the DOM can be manipulated to achieve this).
Syntax of the document.applets
Collection
The document.applets
collection does not have any specific syntax other than being accessed as a property of the document
object:
const appletCollection = document.applets;
The appletCollection
variable will be an HTMLCollection
object.
Key Properties and Methods
The HTMLCollection
object obtained from document.applets
has a few key properties and methods:
Property/Method | Type | Description |
---|---|---|
length |
Number | Returns the number of applet elements in the collection. |
item(index) or [index] |
Function/Syntax | Returns the applet element at the specified index (zero-based). |
namedItem(name) |
Function | Returns the applet element with the given name. |
Note: The HTMLCollection
is a live collection, meaning that changes made to the HTML applets in the document are immediately reflected in the collection. 🔄
Accessing Applet Elements with document.applets
Let's explore some practical examples to demonstrate how to use the document.applets
collection.
Basic Access: Getting the Number of Applets
This example shows how to get the number of <applet>
elements in the document using the length
property.
<!DOCTYPE html>
<html>
<head>
<title>Applet Collection Example</title>
</head>
<body>
<applet id="applet1" code="MyApplet.class" width="200" height="100"></applet>
<applet id="applet2" code="AnotherApplet.class" width="150" height="75"></applet>
<p id="appletCount"></p>
<script></script>
</body>
</html>
Output:
Number of applets: 2
Accessing Applets by Index
This example demonstrates how to access individual applet elements by their index within the HTMLCollection
.
<!DOCTYPE html>
<html>
<head>
<title>Applet Collection Example</title>
</head>
<body>
<applet id="applet3" code="MyApplet.class" width="200" height="100"></applet>
<applet id="applet4" code="AnotherApplet.class" width="150" height="75"></applet>
<p id="appletInfo"></p>
<script>
//<![CDATA[
const applets_example2 = document.applets;
const first_applet_example2 = applets_example2[0];
const second_applet_example2 = applets_example2.item(1);
let appletInfo_text = "First applet ID: " + first_applet_example2.id + ". ";
appletInfo_text += "Second applet ID: " + second_applet_example2.id + ".";
document.getElementById('appletInfo').textContent = appletInfo_text;
//]]]]><![CDATA[>
</script>
</body>
</html>
Output:
First applet ID: applet3. Second applet ID: applet4.
Accessing Applets by Name
This example shows how to access an applet element by its name
attribute using namedItem()
.
<!DOCTYPE html>
<html>
<head>
<title>Applet Collection Example</title>
</head>
<body>
<applet id="applet5" name="myApplet" code="MyApplet.class" width="200" height="100"></applet>
<applet id="applet6" name="anotherApplet" code="AnotherApplet.class" width="150" height="75"></applet>
<p id="namedAppletInfo"></p>
<script></script>
</body>
</html>
Output:
Applet with name 'myApplet' ID: applet5
Iterating Through All Applets
This example demonstrates how to iterate over all applet elements in the document using a for...of
loop.
<!DOCTYPE html>
<html>
<head>
<title>Applet Collection Example</title>
</head>
<body>
<applet id="applet7" code="MyApplet.class" width="200" height="100"></applet>
<applet id="applet8" code="AnotherApplet.class" width="150" height="75"></applet>
<applet id="applet9" code="YetAnotherApplet.class" width="100" height="50"></applet>
<ul id="appletList"></ul>
<script>
//<![CDATA[
const applets_example4 = document.applets;
const appletList_example4 = document.getElementById('appletList');
for (const applet of applets_example4) {
const listItem = document.createElement('li');
listItem.textContent = 'Applet ID: ' + applet.id;
appletList_example4.appendChild(listItem);
}
//]]]]><![CDATA[>
</script>
</body>
</html>
Output:
- Applet ID: applet7
- Applet ID: applet8
- Applet ID: applet9
Example: Modifying Applet Properties
This example illustrates how to access an applet and modify its properties using JavaScript.
<!DOCTYPE html>
<html>
<head>
<title>Applet Collection Example</title>
</head>
<body>
<applet id="applet10" code="MyApplet.class" width="200" height="100"></applet>
<p id="modifiedAppletInfo"></p>
<script></script>
</body>
</html>
Output:
Applet width: 300, height: 150
Note: While the <applet>
tag is deprecated, these examples are useful for demonstrating how to access elements within the DOM using document.applets
. Remember to avoid using <applet>
in modern web development. ⚠️
Use Cases of document.applets
Collection
Despite being linked to a deprecated tag, understanding the document.applets
collection provides valuable insights into DOM manipulation and live collections. This collection and similar live collections (like document.forms
, document.images
, etc.) are primarily used for:
- Legacy Applications: Maintaining and updating older web applications that rely on applets.
- Educational Purposes: Understanding historical web technologies and DOM manipulation techniques.
- Dynamic Scripting: Creating interactive and dynamic web experiences (though modern alternatives are usually preferred).
Browser Support
The document.applets
collection is widely supported by all major web browsers, although it's important to remember that the <applet>
tag itself is deprecated.
Note: While the document.applets
collection is well-supported, using the <applet>
tag itself is discouraged in favor of more modern technologies like JavaScript, WebGL, and WebAssembly. 💡
Conclusion
The document.applets
collection, though associated with a deprecated tag, is a good way to demonstrate how to access and manipulate elements in the DOM using a live HTMLCollection
. It provides a historical insight into web development and can be helpful for maintaining legacy code. Remember to favor modern technologies for new web development projects. Always prioritize using current, well-supported HTML elements and JavaScript features.