18
<html>
 <script type="text/javascript">
 function func() {
 alert(document.getElementById('iView').contentDocument);
 } 
 </script>
 <body>
 <iframe id="iView" style="width:200px;height:200px;"></iframe>
 <a href="#" onclick="func();">click</a>
 </body>
</html>

After click, Firefox returns [object HTMLDocument]. Internet Explorer returns undefined.

How can I select the iView element with Internet Explorer? Thanks.

Donut
113k20 gold badges136 silver badges147 bronze badges
asked Sep 25, 2009 at 14:15
3
  • 4
    @Mauris: he is being polite, and has asked just 10 questions. He'll have time to learn the rules here. Make those comments to those poeple that have asked over 100 questions and have never accepted an anser, or answered a question. Commented Sep 25, 2009 at 14:58
  • meta.stackexchange.com/questions/23475/… Commented Sep 25, 2009 at 15:07
  • 1
    sorry, dont know that this system has 'accept' functionality, thanks :) Commented Sep 28, 2009 at 8:52

6 Answers 6

42

The cross-browser equivalent to contentDocument (including Firefox itself, where contentDocument does work) is contentWindow.document.

So try:

alert(document.getElementById('iView').contentWindow.document);

contentWindow gets you a reference to the iframe's window object, and of course .document is just the DOM Document object for the iframe.

Here's an article that summarizes better.

answered Sep 25, 2009 at 14:45
Sign up to request clarification or add additional context in comments.

1 Comment

worked for me to solve my problem with IE7. newer IE versions seem to support contentDocument.
12

From this page:

Mozilla supports the W3C standard of accessing iframe's document object through IFrameElm.contentDocument, while Internet Explorer requires you to access it through document.frames["name"] and then access the resulting document.

So you need to detect the browser and on IE do something like this instead:

document.frames['iView'].document; 
answered Sep 25, 2009 at 14:21

Comments

3

You seem to want to get the contents of the iframe right?

IE7 and FF2:

var iframe = document.getElementById('iView');
alert(iframe.contentWindow.document.body.innerHTML);
answered Sep 25, 2009 at 14:20

Comments

2

Use feature detection, as contentDocument is supported in IE 8:

var iframe = document.getElementById("iView");
var iframeDocument = null;
if (iframe.contentDocument) {
 iframeDocument = iframe.contentDocument;
} else if (iframe.contentWindow) {
 // for IE 5.5, 6 and 7:
 iframeDocument = iframe.contentWindow.document;
}
if (!!iframeDocument) {
 // do things with the iframe's document object
} else {
 // this browser doesn't seem to support the iframe document object
}
answered Sep 25, 2009 at 14:54

Comments

2
contentWindow.document.body.innerHTML

is working for me in Internet Explorer and Firefox, whereas

contentDocument.body.innerHTML

will only work in Firefox.

uınbɐɥs
7,3535 gold badges29 silver badges42 bronze badges
answered Apr 4, 2012 at 5:06

Comments

1

Do something like this:

var myFrame = document.getElementById('iView');
var frameDoc = myFrame.contentDocument || myFrame.contentWindow;
if (frameDoc.document){
 frameDoc = frameDoc.document;
}
alert(frameDoc);

See this page for more details

answered Nov 8, 2012 at 20:40

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.