How to check if element is being loaded by iframe?

by jasen_gottlieb , in category: Javascript , 4 months ago

How to check if element is being loaded by iframe?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by mallory_cormier , 4 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.