Friday, April 15, 2022

[FIXED] How to check if an element is inside an iframe or not

Issue

Let's say you have a DOM node and you want to know whether it is located inside an iframe or not. One way would be to check it's parent chain to see if you reached an iframe before reaching the parent window. However, I'd like to know if there is a faster way to do this.


Solution

You could probably check the ownerDocument property of the node:

if(node.ownerDocument !== document) {
    // node must be inside iframe
}


Answered By - Felix Kling
Answer Checked By - Cary Denson (PHPFixing Admin)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.