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

- 43,506
- 91
- 209
- 269
-
What do you mean? That's not a function so you wouldn't call it. – Šime Vidas Feb 15 '11 at 16:44
-
http://www.w3schools.com/jsref/prop_frame_contentwindow.asp – Skizit Feb 15 '11 at 16:53
-
I answered this earlier: http://stackoverflow.com/questions/5002334/chrome-getting-iframe-and-inserting-into-body/5002576#5002576 – Tim Down Feb 15 '11 at 16:55
3 Answers
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.

- 81
- 1
- 1
iframe.contentDocument
is what you want.
-
why when using this does.. `.defaultView.getComputedStyle` of the `iframe.contentDocument` say ...`Uncaught TypeError: Cannot call method 'getComputedStyle' of undefined` ? – Skizit Feb 15 '11 at 17:10
-
@Skizit: It works fine for me. Do you have a test page or example code to show the problem? – Tim Down Feb 15 '11 at 17:16
-
@Šime: It does seem to work in the current version of Chrome. I'm not sure it always has though; I think I'd read somewhere that it didn't. `contentDocument` is preferable where it exists anyway. – Tim Down Feb 16 '11 at 00:22
<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>

- 8,102
- 9
- 56
- 91
-
*ownerDocument* is what it sounds like it is. It's the document of the owner of the iFrame, i. e. *document*. – Benjamin Atkin Nov 12 '12 at 18:08