【发布时间】:2018-05-14 16:45:10
【问题描述】:
在下面的代码中:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
<iframe srcdoc="<script>
window.innerStr='xyz';
</script>" >
</iframe>
<script>
function test() {
let iframe = window.frames[0];
console.log('frames innerStr: ' + iframe.innerStr); // does not work if child frame says innerStr = 'xyz' without window scope
}
window.onload = test;
test();
</script>
</body>
</html>
对于window.innerStr='xyz';,在子框架中不使用window范围,父框架无法访问子框架属性(innerStr)
对于 JS 编码,窗口范围是强制性的吗? window.open('file.html') 有类似的结果。
【问题讨论】:
-
iframe.contentWindow || iframe.contentDocument如果满足同源策略要求。 -
@zer00ne 他们都没有
innerStr属性。document.getElementsByTagName('iframe')[0].contentDocument;。是的,同源 -
在跨域文档的情况下,
<iframe>不会让您访问它的框架window对象。所以在这种情况下,唯一的方法是使用postMessage,正如我所说,它将复制正在传输的数据(即不适用于 DOM Element 等复杂对象,并且在一帧中对这些对象所做的更改获胜'不要反映在另一个) -
No 同域,Window 对象
window.open返回类似于ìframe.contentWindow` 一样,在相同的条件下会泄露相同的细节。
标签: javascript iframe ecmascript-5