【发布时间】:2013-04-07 18:27:38
【问题描述】:
在我粘贴任何代码之前,这里是场景:
- 我有一个使用 JavaScript 创建空 iframe 的 HTML 文档
- JavaScript 创建一个函数并将对该函数的引用附加到 iframe 的文档对象(使用
doc.open()获取对文档的引用) - 然后将该函数附加为 iframe 文档的
onload处理程序(通过将<body onload="...">写入 iframe。
现在让我感到困惑的是,onload 处理程序(运行时)内的全局(窗口)和文档对象与通过脚本节点添加的 JavaScript 运行的相同对象不同。
这是 HTML:
<!doctype html>
<html>
<head>
<script>
(function(){
var dom,doc,where,iframe;
iframe = document.createElement('iframe');
iframe.src="javascript:false";
where = document.getElementsByTagName('script')[0];
where.parentNode.insertBefore(iframe, where);
doc = iframe.contentWindow.document;
var _doc = document;
doc.open()._l=function() {
// the window object should be the one that doc is inside
window.vanishing_global=new Date().getTime();
var js = this.createElement("script");
js.src = 'test-vanishing-global.js?' + window.vanishing_global;
window.name="foobar";
this.foobar="foobar:" + Math.random();
document.foobar="barfoo:" + Math.random();
// `this` should be the document object, but it's not
console.log("this == document: %s", this == document);
console.log("this == doc: %s", this == doc);
// the next two lines added based on @Ian's comment below
console.log("_doc == document: %s", _doc == document);
console.log("_doc == doc: %s", _doc == doc);
console.log("name: " + window.name + "\n" + "window.vanishing_global: " + window.vanishing_global + "\ntypeof window.vanishing_global: " + typeof window.vanishing_global + "\ndocument.foobar: " + document.foobar);
this.body.appendChild(js);
};
doc.write('<body onload="document._l();"></body>');
doc.close();
})();
</script>
</head>
<body>
</body>
</html>
这里是test-vanishing-global.js:
console.log("name: " + window.name + "\n" + "window.vanishing_global: " + window.vanishing_global + "\ntypeof window.vanishing_global: " + typeof window.vanishing_global + "\ndocument.foobar: " + document.foobar);
说明:
将这两个文件放到一个目录下,然后在浏览器中打开HTML(在最新的Chrome和Firefox中测试,结果相同)。
这是我得到的输出:
this == document: false
this == doc: true
_doc == document: true
_doc == doc: false
name: foobar
window.vanishing_global: 1366037771608
typeof window.vanishing_global: number
document.foobar: barfoo:0.9013048021588475
name:
window.vanishing_global: undefined
typeof window.vanishing_global: undefined
document.foobar: foobar:0.5015988759696484
处理程序中的this 对象应该是文档对象。它是一个文档对象,但与它在其中运行的文档不同(它也与父文档不同)。处理程序中的 window 对象也与页面中加载的 JavaScript 中运行的 window 对象不同。
所以最后我的问题:
有谁知道发生了什么,以及如何获取对实际窗口对象的引用,或者至少从同一个全局上下文中声明和引用全局变量?
脚注:
此 iframe 不存在跨域问题,因为它们位于同一个域中。如果有人设置document.domain,则会出现问题,但在此示例代码中没有这样做。
【问题讨论】:
-
document是当前页面的document,而不是 iframe 的。 -
您在父页面中声明所有内容。所以对
window和document的引用指向父页面。如果你想对 iframe 做点什么,使用iframe.contentWindow || iframe访问它的window和iframe.contentDocument || iframe.contentWindow.document访问它的document -
@Ian,谢谢。令我困惑的是
_l在 iframe 的上下文中执行,所以document和window不应该根据它们的运行时上下文获取值吗? -
不,范围的唯一 context 是
this。在您的示例中,该方法的所有者是doc,即iframe 的document。除此之外,在此函数中使用已知对象访问的任何内容都是父对象的。如果函数是在不同的地方声明的,但它是在父页面中声明的,那就是另一回事了。
标签: javascript html dom iframe