javascript - Firefox 中的动态创建内容 iframe 问题

标签 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>

最佳答案

我找到了答案

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);
    }

关于javascript - Firefox 中的动态创建内容 iframe 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15038122/

相关文章:

javascript - 如何使用 javascript 和 jQuery 制作列表子项?

javascript - 在 Javascript 中读取/加载 JSON WHERE 'id' = 'x'

firefox - 绝对定位在 Chrome 和 IE 中工作正常,但在 FF 中不行

javascript - GWT:Facebook 应用程序的 Internet Explorer 9 问题

javascript - 重新加载/刷新 iframe 的最佳方法是什么?

javascript - 如何查询谷歌工作表列的某个值

javascript - 我的号码查询功能不起作用,为什么?

google-chrome - Firefox和Chrome参数/首选项列表

javascript - Firefox 在选项卡恢复后触发 onchange 事件

python - html 中的 Selenium 和 iframe