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
How to check if an array includes an object in JavaScript ?
Check if an array includes an object in JavaScript, which refers to determining whether a specific object is present within an array. Since objects are compared by reference, various methods are used to identify if an object with matching properties exists in the array. These are the following appro
3 min read
How to invoke JavaScript code in an iframe from parent page ?
In this article, we will see how to invoke JavaScript code in an iframe from a parent page, along with understanding their implementation through the illustrations. Here, JavaScript methods will be located inside the parent page only but can invoke methods in an iframe and can change or update the c
3 min read
How to check if a string "StartsWith" another string in JavaScript ?
Here are the various methods to check if a string starts with another string in JavaScript 1. Using String.startsWith() MethodThe most widely used method is the startsWith() method, built into JavaScript. This method is simple to use, highly readable, and performs the task efficiently. [GFGTABS] Jav
3 min read
How to Check if an element is a child of a parent using JavaScript?
In this article, we are going to see the methods by which we can Check if an element is a child of a parent using JavaScript. These are the following methods: Table of Content Using the Node.contains() methodLooping through the parents of the given childUsing the hasChildNodes() methodMethod 1: Usin
4 min read
How do I check if an Array includes a value in JavaScript?
To check if an array includes a value we can use JavaScript Array.includes() method. This method returns a boolean value, if the element exists it returns true else it returns false. 1. Using Array.includes() Method - Mostly Used The JS array.includes() method returns true if the array contains the
4 min read
How to check a webpage is loaded inside an iframe or into the browser window using JavaScript?
An iFrame is a rectangular frame or region in the webpage to load or display another separate webpage or document inside it. So basically, an iFrame is used to display a webpage within a webpage. You can see more about iFrames here : HTML iFrames There may be a variety of reasons for checking whethe
4 min read
How to check the current runtime environment is a browser in JavaScript ?
In this article, we will see how to detect the runtime environment in which our JavaScript code is running. Suppose you are building an application in Node.js and need to include that code in a webpage that would be run on a browser. This would cause some functions of a Node application not to work
2 min read
How to Check if a Variable is an Array in JavaScript?
To check if a variable is an array, we can use the JavaScript isArray() method. It is a very basic method to check a variable is an array or not. Checking if a variable is an array in JavaScript is essential for accurately handling data, as arrays have unique behaviors and methods. Using JavaScript
2 min read
How to check a string data type is present in array using JavaScript ?
In JavaScript, an array is a collection of data that can be of the same or different type. If we have the array containing the data, our task is to identify if it is a string data type. In this article, we will learn how to use the typeof operator. Syntax: typeof value Note: The typeof operator retu
3 min read
How to load a hyperlink from one iframe to another iframe ?
HTML Iframe or inline frame allows embedment of multiple HTML pages in an HTML page. In this article, we will see how to change the content of one iframe using hyperlinks in another iframe. This can be achieved by the proper usage of the name attribute in the iframe tag and the target attribute in t
2 min read