How to check if element is being loaded by iframe?

by jasen_gottlieb , in category: Javascript , 7 months ago

How to check if element is being loaded by iframe?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by mallory_cormier , 7 months ago

@jasen_gottlieb 

To check if an element is being loaded by an iframe, you can use the following approach:

  1. Get the parent document object of the element:
1
var parentDoc = element.ownerDocument;


  1. Check if the parent document is the same as the current document. If they are not the same, then the element is being loaded by an iframe:
1
2
3
4
5
if (parentDoc !== document) {
    console.log("Element is being loaded by an iframe");
} else {
    console.log("Element is not being loaded by an iframe");
}


This code snippet determines if the element is being loaded within the current document or in an iframe. If the parent document is different from the current document, it means the element is being loaded by an iframe.

Related Threads:

How to check iframe is loaded in javascript?
How to check if an element is inside an iframe or not?
How to check if dynamic element is visible inside of the iframe?
How to detect whether an iframe is loaded?
How to change content of website loaded in iframe?
How to pause video on iframe when page is loaded?