访问
我试图找出从父页面访问 元素的
window
和 document
属性的最佳方法。 可以通过 JavaScript 创建或通过存储在对象属性或变量中的引用访问,因此,如果我理解正确的话,就排除了
document.frames 的使用
。
我见过很多方法,但我不确定最好的方法。给定以这种方式创建的 :
var iframe = document.createElement('iframe');
document.getElementsByTagName('body')[0].appendChild(iframe);
我目前正在使用它来访问 document
,并且它似乎在主要浏览器上工作正常:
var doc = iframe.contentWindow || iframe.contentDocument;
if (doc.document) {
doc = doc.document;
}
我已经还可以看到这种方法:
var iframe = document.getElementById('my_iframe');
iframe = (iframe.contentWindow) ? iframe.contentWindow :
(iframe.contentDocument.document) ? iframe.contentDocument.document :
iframe.contentDocument;
iframe.document.open();
iframe.document.write('Hello World!');
iframe.document.close();
这让我感到困惑,因为似乎如果定义了 iframe.contentDocument.document ,您最终将尝试访问 iframe.contentDocument.document.document /代码>。
还有这样的:
var frame_ref = document.getElementsByTagName('iframe')[0];
var iframe_doc = frame_ref.contentWindow ? frame_ref.contentWindow.document :
frame_ref.contentDocument;
最后,我想我对哪些属性持有哪些属性感到困惑,contentDocument
是否相当于document
或者是否存在这样的属性contentDocument.document
等。
任何人都可以向我指出有关这些属性的准确/及时的参考,或者快速介绍如何有效访问 ' s
window
和 document
属性以跨浏览器的方式(不使用 jQuery 或其他库)?
感谢您的帮助!
I'm trying to figure out the best way to access an <iframe>
element's window
and document
properties from a parent page. The <iframe>
may be created via JavaScript or accessed via a reference stored in an object property or a variable, so, if I understand correctly, that rules out the use of document.frames
.
I've seen this done a number of ways, but I'm unsure about the best approach. Given an <iframe>
created in this way:
var iframe = document.createElement('iframe');
document.getElementsByTagName('body')[0].appendChild(iframe);
I'm currently using this to access the document
, and it seems to work OK across the major browsers:
var doc = iframe.contentWindow || iframe.contentDocument;
if (doc.document) {
doc = doc.document;
}
I've also see this approach:
var iframe = document.getElementById('my_iframe');
iframe = (iframe.contentWindow) ? iframe.contentWindow :
(iframe.contentDocument.document) ? iframe.contentDocument.document :
iframe.contentDocument;
iframe.document.open();
iframe.document.write('Hello World!');
iframe.document.close();
That confuses me, since it seems that if iframe.contentDocument.document
is defined, you're going to end up trying to access iframe.contentDocument.document.document
.
There's also this:
var frame_ref = document.getElementsByTagName('iframe')[0];
var iframe_doc = frame_ref.contentWindow ? frame_ref.contentWindow.document :
frame_ref.contentDocument;
In the end, I guess I'm confused as to which properties hold which properties, whether contentDocument
is equivalent to document
or whether there is such a property as contentDocument.document
, etc.
Can anyone point me to an accurate/timely reference on these properties, or give a quick briefing on how to efficiently access an <iframe>
's window
and document
properties in a cross-browser way (without the use of jQuery or other libraries)?
Thanks for any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
有一种更简单的方法已经存在了很长时间......使用 window.frames 来获取对框架的窗口对象的引用。
按名称或 ID:
或者如果您愿意:
或按索引:
这里是
window.frames
的良好参考:http://developer.mozilla.org/en/DOM/window.frames摘录:
There's an easier way that's been around longer... use
window.frames
to get a reference to the frame's window object.By name or id:
or if you prefer:
or by index:
Good reference for
window.frames
here: http://developer.mozilla.org/en/DOM/window.framesAn excerpt:
在这段时间里,我做了很多测试,最后想出了这个简短但强大的语法,它适用于我可以测试的每个浏览器:
编辑: @DaggNabbit 注意到
iframe.contentWindow 中存在引用错误.document
,如果未设置iframe.contentWindow
,则会阻止代码执行,不允许返回iframe.document
。所以我改进了我的代码:
注意:
iframe.document
是 IE5 的解决方法。During the time I made many tests, and finally came up with this short but robust syntax which works on every browser I could test:
EDIT: @DaggNabbit noticed that a reference error in
iframe.contentWindow.document
, ififrame.contentWindow
is not set, would block the code execution, not allowingiframe.document
to be returned.So I refined my code:
NOTE:
iframe.document
is a workaround for IE5.除 Chrome 之外的所有现代浏览器都支持
iframereference.contentWindow
和iframereference.contentDocument
,但前提是在 iframe 中打开的页面与包含iframe。要包含 Chrome,请使用 var iwin=iframereference.contentWindow,
idoc=iwin.document;
.contentDocument
是 iframe 中页面的window.document
,与
contentWindow.document
相同,但不是.contentDocument.document
。Chrome 可能会在未来的某个版本中支持
.contentDocument
- 我希望如此,因为这也是所有其他浏览器查找 Object 元素中包含的文档的方式,输入text/html< /code>,其中 data 属性是 html 页面的 url。
all of the modern browsers except Chrome support both
iframereference.contentWindow
andiframereference.contentDocument
, but only if the page opened in the iframe is on the same domain as the page containing the iframe.To include Chrome, use
var iwin=iframereference.contentWindow,
idoc=iwin.document;
.contentDocument
is thewindow.document
of the page in the iframe,as is
contentWindow.document
, but not.contentDocument.document
.Chrome may include support for
.contentDocument
in some future version- I hope so, because it is also the way all the other browsers find the document contained in an Object element, typetext/html
, where the data attribute is the url of an html page.我认为这是一种非常简单且跨浏览器的
测试方式
i think this is a very easy and cross browser way
you can test it