最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - enable iframe's design mode - Stack Overflow

programmeradmin3浏览0评论

i have an iframe and in javascript i enable the design mode as follows

iframe.contentDocument.designMode="on";

which works fine if i create my iframe inline. but if i load my iframe with a "src" attribute in my parent page and i turn on the design mode as follows:

iframes=document.getElementsByTagName('iframe');
for(iframe in iframes)
{
iframes[iframe].contentDocument.designMode="on";
}

then it doesnot work. how can i enable the iframe's design mode from the container page if im loading the iframe specifying the src attribute? not inline!! Thanks

i have an iframe and in javascript i enable the design mode as follows

iframe.contentDocument.designMode="on";

which works fine if i create my iframe inline. but if i load my iframe with a "src" attribute in my parent page and i turn on the design mode as follows:

iframes=document.getElementsByTagName('iframe');
for(iframe in iframes)
{
iframes[iframe].contentDocument.designMode="on";
}

then it doesnot work. how can i enable the iframe's design mode from the container page if im loading the iframe specifying the src attribute? not inline!! Thanks

Share Improve this question edited Aug 3, 2011 at 10:23 samach asked Aug 3, 2011 at 10:12 samachsamach 3,39410 gold badges43 silver badges54 bronze badges 2
  • Are you loading the iframe's contents from the same domain as the parent/containing site? If it's from a different site you will have no access to the DOM of the iframe. – Jon Grant Commented Aug 3, 2011 at 10:26
  • yea im loading it from the same domian...and still i cant modify the contentDocument properties of the loaded iframe – samach Commented Aug 3, 2011 at 10:29
Add a ment  | 

1 Answer 1

Reset to default 8

Depending on the browser, there are a couple of potential issues:

  • not all browsers (particularly older ones) support contentDocument
  • for...in is not guaranteed to work on a NodeList, which is what getElementsByTagName() returns. Use for loop instead.
  • you may need to wait for all of the iframes to load. The main document's load event will fire only once all the iframes have loaded.

Otherwise, so long as the iframe is served from the same domain, it is possible to set its document's designMode property from the containing document. The following should work:

window.onload = function() {
    var iframes = document.getElementsByTagName('iframe');
    for (var i = 0, len = iframes.length, doc; i < len; ++i) {
        doc = iframes[i].contentDocument || iframes[i].contentWindow.document;
        doc.designMode = "on";
    }
};
发布评论

评论列表(0)

  1. 暂无评论