访问

发布于 2024-09-04 01:55:12 字数 1625 浏览 1 评论 0原文

我试图找出从父页面访问 元素的 windowdocument 属性的最佳方法。 可以通过 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 windowdocument 属性以跨浏览器的方式(不使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

非要怀念 2024-09-11 01:55:12

有一种更简单的方法已经存在了很长时间......使用 window.frames 来获取对框架的窗口对象的引用。

按名称或 ID:

var iframe_doc = window.frames.my_iframe.document;

或者如果您愿意:

var iframe_doc = window.frames['my_iframe'].document;

或按索引:

var iframe_doc = window.frames[0].document;

这里是 window.frames 的良好参考:http://developer.mozilla.org/en/DOM/window.frames

摘录:

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:

var iframe_doc = window.frames.my_iframe.document;

or if you prefer:

var iframe_doc = window.frames['my_iframe'].document;

or by index:

var iframe_doc = window.frames[0].document;

Good reference for window.frames here: http://developer.mozilla.org/en/DOM/window.frames

An excerpt:

each item in the window.frames
pseudo-array represents the window
object corresponding to the given
<frame>'s or <iframe>'s content, not
the (i)frame DOM element (i.e.
window.frames[ 0 ] is the same thing
as document.getElementsByTagName(
"iframe" )[ 0 ].contentWindow)

梦里兽 2024-09-11 01:55:12

在这段时间里,我做了很多测试,最后想出了这个简短但强大的语法,它适用于我可以测试的每个浏览器:

var doc = iframe.contentDocument ?
iframe.contentDocument : (iframe.contentWindow.document || iframe.document);

编辑: @DaggNabbit 注意到 iframe.contentWindow 中存在引用错误.document,如果未设置iframe.contentWindow,则会阻止代码执行,不允许返回iframe.document
所以我改进了我的代码:

var doc = iframe.contentDocument ?
    iframe.contentDocument :
    (iframe.contentWindow ? iframe.contentWindow.document : 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:

var doc = iframe.contentDocument ?
iframe.contentDocument : (iframe.contentWindow.document || iframe.document);

EDIT: @DaggNabbit noticed that a reference error in iframe.contentWindow.document, if iframe.contentWindow is not set, would block the code execution, not allowing iframe.document to be returned.
So I refined my code:

var doc = iframe.contentDocument ?
    iframe.contentDocument :
    (iframe.contentWindow ? iframe.contentWindow.document : iframe.document);

NOTE: iframe.document is a workaround for IE5.

万劫不复 2024-09-11 01:55:12

除 Chrome 之外的所有现代浏览器都支持 iframereference.contentWindowiframereference.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 and iframereference.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 the window.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, type text/html, where the data attribute is the url of an html page.

围归者 2024-09-11 01:55:12

我认为这是一种非常简单且跨浏览器的

//get document object of IFRAME
if(typeof frame.contentDocument!='undefined') {
    iframeDocument=frame.contentDocument;
} else {
    iframeDocument=frame.document;
}
//get window object of IFRAME
if(typeof frame.contentWindow!='undefined') {
    iframeWindow=frame.contentWindow;
} else {
    iframeWindow=frame.window;
}

测试方式

i think this is a very easy and cross browser way

//get document object of IFRAME
if(typeof frame.contentDocument!='undefined') {
    iframeDocument=frame.contentDocument;
} else {
    iframeDocument=frame.document;
}
//get window object of IFRAME
if(typeof frame.contentWindow!='undefined') {
    iframeWindow=frame.contentWindow;
} else {
    iframeWindow=frame.window;
}

you can test it

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文