admin管理员组

文章数量:1287577

I've got the following code which works fine in Firefox...

if (!iFrame) iFrame = outerDoc.getElementById('_dialog_iframe');
var iFrameDoc = iFrame.contentWindow.document; // get iframe doc

and the Chrome version...

if (!iFrame) iFrame = outerDoc.getElementById('_dialog_iframe');
var iFrameDoc = iFrame.document; // get iframe doc

I'm testing the code be getting iFrameDoc.body when I run the FireFox code in Firefox it works fine. However, the Chrome code returns undefined. Why? How do I fix this so that it'll work fine in Chrome?

I've got the following code which works fine in Firefox...

if (!iFrame) iFrame = outerDoc.getElementById('_dialog_iframe');
var iFrameDoc = iFrame.contentWindow.document; // get iframe doc

and the Chrome version...

if (!iFrame) iFrame = outerDoc.getElementById('_dialog_iframe');
var iFrameDoc = iFrame.document; // get iframe doc

I'm testing the code be getting iFrameDoc.body when I run the FireFox code in Firefox it works fine. However, the Chrome code returns undefined. Why? How do I fix this so that it'll work fine in Chrome?

Share Improve this question asked Feb 15, 2011 at 10:16 SkizitSkizit 44.9k93 gold badges213 silver badges270 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 11

If the iframe element has a document property in Chrome then I'm surprised, and it's non-standard and not supported in other browsers. The standards-based property is contentDocument, and to support other browsers you can use contentWindow.document. The following will work in all major browsers:

var iFrameDoc = iFrame.contentDocument || iFrame.contentWindow.document;

本文标签: javascriptChrome Getting iFrame and inserting into bodyStack Overflow