【发布时间】:2011-04-29 06:50:23
【问题描述】:
我有这个 HTML 代码:
<html>
<head>
<script type="text/javascript">
function GetDoc(x)
{
return x.document ||
x.contentDocument ||
x.contentWindow.document;
}
function DoStuff()
{
var fr = document.all["myframe"];
while(fr.ariaBusy) { }
var doc = GetDoc(fr);
if (doc == document)
alert("Bad");
else
alert("Good");
}
</script>
</head>
<body>
<iframe id="myframe" src="http://example.com" width="100%" height="100%" onload="DoStuff()"></iframe>
</body>
</html>
问题是我收到消息“坏”。也就是说iframe的文档没有正确获取,GetDoc函数返回的其实是父文档。
如果你能告诉我我的错误在哪里,我将不胜感激。 (我想将文档托管在 IFrame 中。)
谢谢。
【问题讨论】:
-
这是 2010 年提出的,今天是 2015 年,这在任何更新的浏览器中都不起作用,除非您正在开发 google.com。由于跨源策略,如果 iframe 指向的页面与加载原始文档的域不同,则您无法访问它的内容。
标签: javascript html iframe document