Sometimes, we want to access parent iframe from JavaScript.
In this article, we’ll look at how to access parent iframe from JavaScript.
How to access parent iframe from JavaScript?
To access parent iframe from JavaScript, we get the parent document and then use that to get the parent iframe.
For instance, we write
const arrFrames = parent.document.getElementsByTagName("IFRAME");
for (const arrFrame of arrFrames) {
if (arrFrame === window) {
console.log("parent frame found");
}
}
to loop through the iframes returned by getElementsByTagName
from the parent document
.
And then we loop through the frames with a for-of loop.
In it, we check if arrFrame
equals window
.
If it is, then we found the parent frame.
Conclusion
To access parent iframe from JavaScript, we get the parent document and then use that to get the parent iframe.