I have 2 iframe on my webpage. iframe S has the same origin as the web page, iframe D has a different one:
<iframe src="https://mdn-samples.mozilla.org/snippets/html/iframe-simple-contents.html" title="iframe example 1" width="400" height="300">
<p>Your browser does not support iframes.</p>
</iframe>
I need to addEventListener only onto the iframes that have the same origin. This is what I do:
for (let i = 0; i < frames.length; i++) {
const iframe = window.frames[I]
console.log(iframe)
if (iframe.document.domain === window.document.domain) {
iframe.addEventListener('message', event => {
console.log(event.data)
}, false)
}
}
But this gives an error, when I try to read iframe.document of iframe D.
Uncaught DOMException: Blocked a frame with origin "http://localhost:8080" from accessing a cross-origin frame.
Because, unlike iframe S, iframe D gives me this object, when I try console.log(iframe) from the 2 snippet of code above:
And as you can see, there is no document property.