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 whether a webpage is loaded in an iFrame, for example, in cases where we need to dynamically adjust the height or width of an element.
- Comparing the object’s location with the window object’s parent location: Here, we simply compare the object’s location with the window object’s parent location. If the result is true, then the webpage is in an iFrame. If it is false, then it is not in an iFrame.
<script>functioniniFrame() {if( window.location !== window.parent.location ){// The page is in an iFramesdocument.write("The page is in an iFrame");}else{// The page is not in an iFramedocument.write("The page is not in an iFrame");}}// Calling iniFrame functioniniFrame();</script>chevron_rightfilter_noneOutput:
The page is in an iFrame
- Using window.top and window.self property: The top and self are both window objects, along with parent, so check if the current window is the top/main window.
<script>// Function to check whether webpage is in iFramefunctioniniFrame() {if(window.self !== window.top) {// !== operator checks whether the operands// are of not equal value or not equal typedocument.write("The page is in an iFrame");}else{document.write("The page is in an iFrame");}}// Calling iniFrame functioniniFrame();</script>chevron_rightfilter_noneOutput:
The page is in an iFrame
- Using the window.frameElement property: Note that this only supports webpages belonging to the same origin as the main page in which it is embedded. The function window.frameElement returns the element (like iframe and object) in which the webpage is embedded.
<script>functioniniFrame() {vargfg = window.frameElement;// Checking if webpage is embeddedif(gfg) {// The page is in an iFramedocument.write("The page is in an iFrame");}else{// The page is not in an iFramedocument.write("The page is not in an iFrame");}}// Calling iniFrame functioniniFrame();</script>chevron_rightfilter_noneOutput:
The page is in an iFrame
In the above code, store the element in which the webpage is embedded to the variable gfg. If the window isn’t embedded into another document, or if the document into which it’s embedded has a different origin (such as having been located from a different domain), gfg is null.
Recommended Posts:
- How to check whether the background image is loaded or not using JavaScript ?
- How to close current tab in a browser window using JavaScript?
- Cross-browser window resize event using JavaScript/jQuery
- How to redirect browser window back using JavaScript ?
- How to run a function when the page is loaded in JavaScript ?
- How to create a JavaScript callback for knowing if an image is loaded ?
- How to place a div inside an iframe for IE ?
- Check if a jquery has been loaded or not
- How to Check Twitter Bootstrap is Loaded or not ?
- How to check if a jQuery plugin is loaded?
- How to check whether an image is loaded or not ?
- How to give a div tag 100% height of the browser window using CSS
- How to get the size of screen, current web page and browser window using jQuery?
- Hide the cursor in a webpage using CSS and JavaScript
- How to redirect to another webpage using JavaScript?
- Call a parent window function from an iframe
- How to see the extensions loaded by PHP ?
- How to insert HTML content into an iFrame using jQuery?
- How to get HTML content of an iFrame using JavaScript ?
- How to change the width of an iframe to 100% using JavaScript ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


