【发布时间】:2018-03-07 18:30:07
【问题描述】:
我的网页上有 2 个 iframe。 iframe S与网页同源,iframe D不同:
<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>
我只需要将事件监听器添加到具有相同来源的 iframe 上。我就是这样做的:
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)
}
}
但是,当我尝试读取 iframe D 的 iframe.document 时,会出现错误。
Uncaught DOMException: Blocked a frame with origin "http://localhost:8080" from accessing a cross-origin frame.
因为,与 iframe S 不同,当我从 2 sn 尝试 console.log(iframe) 时,iframe D 给了我这个对象上面代码的-p:
如您所见,没有文档属性。
【问题讨论】:
标签: javascript html dom iframe