How to work with document.embeds in JavaScript ?
Last Updated :
01 May, 2024
The document. embeds is a property in JavaScript that provides access to all embedded elements such as <embed> and <object> tags within the current document. These embedded elements are typically used to display external content, such as media files (e.g., videos, audio) or interactive content (e.g., Flash animations).
Below are the approaches to show how you can work with the document. embeds property:
Accessing Embedded Elements
Accessing embedded elements involves using the document. embeds property, which returns a collection of all embedded elements (<embed> and <object> tags) within the current document. This collection can be iterated over to work with individual embedded elements.
Example: The below code practically implements the document.embeds property to access all embedded elements.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content=
"width=device-width, initial-scale=1.0">
<title>Embedded Videos</title>
</head>
<body>
<h1>Embedded Videos</h1>
<embed width="560" height="315" src=
"https://www.youtube.com/embed/oM0HOxae8_A?si=Hp6UlyupbrZfF_iS"
title="YouTube video player" frameborder="0"
allow="accelerometer; autoplay; clipboard-write;
encrypted-media; gyroscope; picture-in-picture; web-share"
referrerpolicy="strict-origin-when-cross-origin"
allowfullscreen>
</embed>
<embed width="560" height="315" src=
"https://www.youtube.com/embed/mpIDwAT7m_0?si=sUB4S0AkGtwvuhDW"
title="YouTube video player" frameborder="0"
allow="accelerometer; autoplay; clipboard-write;
encrypted-media; gyroscope; picture-in-picture; web-share"
referrerpolicy="strict-origin-when-cross-origin"
allowfullscreen>
</embed>
<div id="embedInfo">
</div>
<script>
function displayEmbedInfo() {
let embeds = document.embeds;
let embedInfoDiv =
document.getElementById('embedInfo');
embedInfoDiv.innerHTML = '';
let embedCount = embeds.length;
console.log(embedCount)
let embedCountText =
document.createElement('p');
embedCountText.textContent =
'Number of embedded elements: ' + embedCount;
embedInfoDiv.appendChild(embedCountText);
for (let i = 0; i < embedCount; i++) {
let embed = embeds[i];
let embedParameters = document.createElement('p');
embedParameters.textContent =
'Embedded Element ' + (i + 1) + ' - Type: ' +
embed.tagName + ', Source: ' + embed.src;
embedInfoDiv.appendChild(embedParameters);
}
}
window.onload = displayEmbedInfo;
</script>
</body>
</html>
Output:

Working with Embedded Elements
Once you have access to an embedded element, you can interact with it by accessing its properties and methods. The specific properties and methods available depend on the type of embedded element and its implementation. You can manipulate embedded elements by controlling their playback, changing their attributes, etc.
Example: The below code will show the use of embeds property to work with embedded elements.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, initial-scale=1.0">
<title>Embedded YouTube Video</title>
<script src="https://www.youtube.com/iframe_api"></script>
</head>
<body>
<h1>Working with Embedded Elements</h1>
<!-- This div will be replaced with the
YouTube iframe once the API is loaded -->
<div id="youtube-player"></div>
<script>
let player;
// This function is called once the YouTube Iframe API is ready
function onYouTubeIframeAPIReady() {
player = new YT.Player('youtube-player', {
width: 560,
height: 315,
videoId: 'oM0HOxae8_A', // The YouTube video ID
playerVars: {
autoplay: 1,
},
events: {
// You can add other event handlers here if needed
}
});
}
// Pause the video after 5 seconds
setTimeout(function() {
if (player && typeof player.pauseVideo === 'function') {
player.pauseVideo();
}
}, 5000);
</script>
</body>
</html>
Output:
outputThis HTML snippet embeds a YouTube video that pauses after 5 seconds. It loads the YouTube Iframe API in the head section and sets up a div in the body where the YouTube player will be created. The onYouTubeIframeAPIReady() function initializes the YouTube player with a specific video ID (oM0HOxae8_A) and sets it to autoplay. A setTimeout function pauses the video after 5 seconds by calling player.pauseVideo(), ensuring the YouTube player is properly initialized and has the pause functionality. This setup creates a YouTube video that automatically stops after a brief delay.
Similar Reads
How does inline JavaScript work with HTML ? Inline JavaScript refers to JavaScript code embedded directly within HTML elements using the onclick, onmouseover, or other event attributes. This allows you to execute JavaScript code in response to user interactions or specific events without needing a separate JavaScript file or script block.Synt
2 min read
How to Display Word Document in HTML using JavaScript? Displaying Word documents in HTML is a valuable feature for web applications, enabling users to view content without needing to download files. JavaScript provides several tools and libraries to facilitate this process, making it easier to convert and render .docx files directly in the browser. This
4 min read
How to Add JavaScript in HTML Document? To add JavaScript in HTML document, several methods can be used. These methods include embedding JavaScript directly within the HTML file or linking an external JavaScript file.Inline JavaScriptYou can write JavaScript code directly inside the HTML element using the onclick, onmouseover, or other ev
3 min read
How to use Ejs in JavaScript ? EJS or Embedded Javascript Templating is a templating engine used by Node.js. The template engine helps to create an HTML template with minimal code. Also, it can inject data into the HTML template on the client side and produce the final HTML. Installation StepsInstall the module using the followin
3 min read
How to Add JavaScript in WordPress Page and Post? WordPress doesnât allow you to directly add JavaScript code to your site, you can achieve this by using plugins or manual methods. JavaScript is a popular programming language that allows you to add interactive features to your website. Learning how to add JavaScript to your WordPress site can help
3 min read
How to Create an Image Element using JavaScript? Creating an image element dynamically using JavaScript is a useful technique when you want to add images to a webpage without having to manually write the <img> tag in your HTML. This allows for more flexibility, as we can create and manipulate images based on user interactions or other condit
2 min read
How to Fix "ReferenceError: document is not defined" in JavaScript? The "ReferenceError: document is not defined" error in JavaScript is a common issue that occurs when trying to access the document object outside the browser environment such as in Node.js. This error can also occur if the script is executed before the HTML document is fully loaded. In this article,
2 min read
How are DOM utilized in JavaScript ? What is DOM The document object Model (DOM) represents the full HTML document. When an HTML document is loaded within the browser, it becomes a document object. The root element represents the HTML document, its properties, and its methods. With the assistance of a document object, we will add dynam
3 min read
DOM Loading Issues with JavaScript DOM loading issues in JavaScript typically occur when your scripts are trying to interact with elements that havenât fully loaded yet. This can cause errors or unexpected behavior. Common CausesScripts Loading Before DOM is Ready: If your JavaScript is placed in the <head> section or before th
2 min read
JavaScript - How to Return Current URL for Share Button? In this article, we will learn how to create share buttons on websites, allowing users to share a post or the URL of the site on various social media platforms. We will break this down into simple steps for better understanding.Step 1: Create the HTML StructureFirst, we need to create an HTML file t
4 min read