从主文档中的 JavaScript 获取 IFrame 的文档
我有这个 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>
问题是我收到消息“Bad”。这意味着没有正确获取iframe的文档,而GetDoc函数实际返回的是父文档。
如果您告诉我我的错误在哪里,我将不胜感激。 (我想要将文档托管在 IFrame 中。)
谢谢。
I have this HTML code:
<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>
The problem is that I get message "Bad". That mean that the document of iframe is not got correctly, and what is actualy returned by GetDoc function is the parent document.
I would be thankful, if you told where I do my mistake. (I want to get document hosted in IFrame.)
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您应该能够使用以下代码访问 IFRAME 中的文档:
但是,如果框架中的页面是从不同的域(例如 google.com)加载的,您将无法执行此操作。这是因为浏览器的同源政策。
You should be able to access the document in the IFRAME using the following code:
However, you will not be able to do this if the page in the frame is loaded from a different domain (such as google.com). This is because of the browser's Same Origin Policy.
问题是,在 IE 中(我猜你正在测试的是 IE),
元素有一个
document
属性,该属性引用包含以下内容的文档: iframe,并且它在contentDocument
或contentWindow.document
属性之前使用。您需要的是:此外,
document.all
并非在所有浏览器中都可用并且是非标准的。请改用document.getElementById()
。The problem is that in IE (which is what I presume you're testing in), the
<iframe>
element has adocument
property that refers to the document containing the iframe, and this is getting used before thecontentDocument
orcontentWindow.document
properties. What you need is:Also,
document.all
is not available in all browsers and is non-standard. Usedocument.getElementById()
instead.如果您遇到跨域错误:
如果您可以控制 iframe 的内容 - 也就是说,如果它只是在跨域设置中加载,例如在 Amazon Mechanical Turk 上 - 您可以使用 < code> 内部 html 属性。
例如,对于内部 html,使用
this
html 参数(是的 -this
已定义,它引用内部 body 元素的父窗口):在内部 html 中:
In case you get a cross-domain error:
If you have control over the content of the iframe - that is, if it is merely loaded in a cross-origin setup such as on Amazon Mechanical Turk - you can circumvent this problem with the
<body onload='my_func(my_arg)'>
attribute for the inner html.For example, for the inner html, use the
this
html parameter (yes -this
is defined and it refers to the parent window of the inner body element):<body onload='changeForm(this)'>
In the inner html :
您还可以使用:
You can also use: