How to check if an iframe is present in another iframe in JavaScript ?
Last Updated :
24 Apr, 2025
In this article, we will see how to check if an iframe is present in another iframe, along with understanding its basic implementation with the help of a simple example.
An iFrame is an HTML element that allows users to embed a webpage within another webpage. It's frequently used for displaying content from another website, such as a video or a social media widget. iFrames can be nested inside each other, which means that you can have an iFrame within another iFrame. Sometimes, you may need to check if an iFrame is present in another iFrame, which can be a bit tricky. In this article, we will show you how to do that using JavaScript.
Before we proceed, we will first understand how an iFrame works. An iFrame is essentially a separate HTML document embedded within another HTML document. When the browser encounters an iFrame element, it creates a new browsing context (i.e., a new window or tab) and loads the specified URL into that context. The contents of the iFrame are displayed within the original document, but they are technically separate.
Iframes are commonly used in the following scenarios:
- Embedding external content: If you want to embed content from a third-party website, such as a YouTube video or a social media post, you can use an iframe.
- Displaying content from other pages: If you want to display content from other pages of your website, you can use an iframe.
- Isolating content: If you want to isolate content from the rest of your web page, such as a login form or a shopping cart, you can use an iframe.
Approach: When an iFrame is present in another iFrame, the embedded document can be accessed through the contentWindow property of the outer iFrame. The contentWindow property returns a reference to the window object of the embedded document. By checking the contentWindow property of an iFrame, we can find if it contains another iFrame.
var outerIframe = document.getElementById('outer-iframe');
Now we have a reference to the outer iFrame, we can use the contentWindow property to access the embedded document and get a reference to the inner iFrame element.
- Use the contentWindow to get a reference to the Inner Iframe
var innerIframe = outerIframe.contentWindow.document.getElementById('inner-iframe');
- Checking if the innerIframe is present in the OuterIframe
Now that we have a reference to the inner iFrame element, we can check if it is present in the outer iFrame. To do this, we need to check if the innerIframe variable is not null and has a tag name of 'iframe'. We can use the tagName property of the element to check its tag name. For example:
if (outerIframe.contentWindow.document.contains(innerIframe)) {
console.log('Inner iframe is present in the Outer iframe.');
} else {
console.log('Inner iframe is not present in the Outer iframe.');
}
Example: In the following example, we will create an HTML file with an iframe in it, we will also embed another iframe in this iframe, Now we will use the above-mentioned approach to find if the iframe is present in another iframe. we will add the javascript code to check if an iframe is present in another iframe in the script tags in the main HTML file.
HTML code with nested iframes: In the below code, The window.addEventListener('load',..) will make sure that the code inside the script tags will run only after the document is completely loaded.
HTML
<!DOCTYPE html>
<html>
<script>
window.addEventListener('load', function () {
// Get the outer iframe
var outerIframe = document.getElementById('outerIframe');
// Get the inner iframe
var innerIframe =
outerIframe.contentWindow.document.getElementById('innerIframe');
// Check if the inner iframe is present in the outer iframe
if (outerIframe.contentWindow.document.contains(innerIframe)) {
console.log('Inner iframe is present in the outer iframe.');
} else {
console.log('Inner iframe is not present in the Outer iframe.');
}
});
</script>
<head>
<title>
Check if an iframe is present in another iframe
</title>
</head>
<body style="text-align: center;">
<h1 style="color: green;">
GeeksforGeeks
</h1>
<h3>
How to check if an iframe is
present in another iframe?
</h3>
<iframe width="600"
height="400"
id="outerIframe"
src="outer.html">
</iframe>
</body>
</html>
HTML
<!DOCTYPE html>
<html>
<head>
<title>Outer Iframe</title>
</head>
<body>
<p>This is Outer iFrame</p>
<iframe width="400"
height="300"
id="innerIframe"
src="inner.html">
</iframe>
</body>
</html>
- inner.html: In the below file, when the inner iframe is loaded completely we will call the checkIframe() method to check if the iframe is present inside another iframe.
HTML
<!DOCTYPE html>
<html>
<head>
<title>Inner Iframe</title>
</head>
<body>
<p>Hello Geeks, This is Inner iframe!</p>
</body>
</html>
Output:
Similar Reads
Non-linear Components
In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial
JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. JavaScript is an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side : On client sid
11 min read
Web Development
Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Class Diagram | Unified Modeling Language (UML)
A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Spring Boot Tutorial
Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
React Interview Questions and Answers
React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
HTML Tutorial
HTML stands for HyperText Markup Language. It is the standard language used to create and structure content on the web. It tells the web browser how to display text, links, images, and other forms of multimedia on a webpage. HTML sets up the basic structure of a website, and then CSS and JavaScript
10 min read
Backpropagation in Neural Network
Backpropagation is also known as "Backward Propagation of Errors" and it is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network. In this article we will explore what
10 min read
JavaScript Interview Questions and Answers
JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. Both front-end and back-end developers need to have a strong command of JavaScript, as
15+ min read
AVL Tree Data Structure
An AVL tree defined as a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees for any node cannot be more than one. The absolute difference between the heights of the left subtree and the right subtree for any node is known as the balance factor of
4 min read