iframe.contentWindow.document Chrome alternative?

2019-04-28 17:35发布

How would I call the following in Chrome? iframe.contentWindow.document

3条回答
闹够了就滚
2楼-- · 2019-04-28 18:24

iframe.contentDocument is what you want.

See also Chrome: Getting iFrame and inserting into body

查看更多
Deceive 欺骗
3楼-- · 2019-04-28 18:25
<iframe id="popupFrame" />

<script type="text/javascript">
var popupFrame = document.getElementById('popupFrame');
var iFrameDoc;
if (switchAccountsPopupFrame.document) {
    iFrameDoc = switchAccountsPopupFrame.document; // works with IE9 and FF9
}
else {
    // Google Chrome (16.0.912.75 m)
    iFrameDoc = switchAccountsPopupFrame.ownerDocument;
}
....
</script>
查看更多
我欲成王,谁敢阻挡
4楼-- · 2019-04-28 18:39

Chrome does in fact support iframe.contentWindow.document, but there's a little wrinkle that you've probably fallen prey to: if the file set to the iframe.src property is being accessed locally (i.e. using the "file://" protocol), that property is inaccessible in Chrome. This will happen if you specify a relative file address and attempt to test the script without using a web-server like IIS or Apache (by simply double-clicking it). The same also applies to iframe.contentDocument.

I ran into a similar problem where, for some odd reason, Chrome would not accept event handlers dynamically attached to iframes. Then I found the note in this article, tested with Apache, and hey presto, it suddenly began to work.

查看更多
登录 后发表回答