在Firefox中动态创建内容iframe问题

时间:2013-02-23 07:10:19

标签: javascript firefox iframe

我已经编写了一个javascript函数来创建简单的内容iframe,它在所有浏览器IE9,8,7和chrome中工作正常但在mozilla firefox中不起作用,我的代码有什么问题?甚至没有在控制台中得到任何异常。

function (parent, child, cssfile, jsfilepath) {

        var iframe = document.createElement('iframe');
        iframe.id = ('MyID' + Math.floor((Math.random() * 1000000000000000000) + 1));
        iframe.frameBorder = '0';
        iframe.scrolling = 'no';
        iframe.marginWidth = '0';
        iframe.marginHeight = '0';
        iframe.hspace = '0';
        iframe.vspace = '0';
        iframe.allowTransparency = "true";

        parent.appendChild(iframe);

        var iframeDoc = iframe.contentDocument || iframe.contentWindow.document;

        var inter = window.setInterval(function() {
             if(iframeDoc.readyState == "complete") {
                 window.clearInterval(inter);

                 iframeDoc.body.innerHTML = child;

                 iframeDoc.body.style.background = "transparent";

                 addExternalCss(cssfile, iframeDoc);
                 addEmbedCss('body {margin:0px; padding:0px;}', iframeDoc);
                 addJs(jsfilepath, iframeDoc);
             }
         },100);
    }

修改

在firebug中显示空白iframe

<iframe scrolling="no" frameborder="0" id="MyId350236077714409500" marginwidth="0" marginheight="0">

<html><head></head><body></body></html>

</iframe>

1 个答案:

答案 0 :(得分:1)

我找到了答案

function (parent, child, cssfile, jsfilepath) {

        var iframe = document.createElement('iframe');
        iframe.id = ('MyID' + Math.floor((Math.random() * 1000000000000000000) + 1));
        iframe.frameBorder = '0';
        iframe.scrolling = 'no';
        iframe.marginWidth = '0';
        iframe.marginHeight = '0';
        iframe.hspace = '0';
        iframe.vspace = '0';
        iframe.allowTransparency = "true";

        parent.appendChild(iframe);



        var inter = window.setInterval(function() {
             // put inside function 
             var iframeDoc = iframe.contentDocument || iframe.contentWindow.document;

             if(iframeDoc.readyState == "complete") {
                 window.clearInterval(inter);

                 iframeDoc.body.innerHTML = child;

                 iframeDoc.body.style.background = "transparent";

                 addExternalCss(cssfile, iframeDoc);
                 addEmbedCss('body {margin:0px; padding:0px;}', iframeDoc);
                 addJs(jsfilepath, iframeDoc);
             }
         },100);
    }