代码之家  ›  专栏  ›  技术社区  ›  Drew Rich

parent.iframedpdf未定义

  •  0
  • Drew Rich  · 技术社区  · 16 年前

    在Firefox中使用下面的代码时,我会得到“parent.iframedpdf is undefined”javascript错误,在IE中会出现“object expected”错误。

    有人能告诉我我做错了什么吗?谢谢

    function PrintFrame(xFile){
        parent.iframePDF.location.href = xFile;
        document.getElementById("spanMess").style.display = "block";
    
        parent.iframePDF.onload = new function() {
            setTimeout("parent.iframePDF.print();parent.document.getElementById('spanMess').style.display='none';",10000);
        }
    }
    
    <iframe id="iframePDF" src="about:blank" style="display:none"></iframe>
    
    <input type="image" onclick="PrintFrame('525.pdf')" src="../../cms_res/DisneyChannel/playhouse/images/buttons/printer.png" />
    
    <span id="spanMess" style="display:none;color:red;">
        <h3>Preparing Document For Print</h3>
    </span>
    
    3 回复  |  直到 16 年前
        1
  •  1
  •   Lekensteyn    16 年前

    您应该使用name=“iframedpdf”而不是id=“iframedpdf”。 这样,您就可以使用window.iframedpdf(->window.frames.iframedpdf)。

    如果使用document.getElementByID(“iframedpdf”),则不能在上面使用.location.href。

    编辑:蒂姆是对的,这里不需要父母。

        2
  •  0
  •   jerjer    16 年前

    您的代码可能无法在其他浏览器上成功调用printframe,问题是iframe上的onload事件可能不再触发。

    您可以尝试以下操作:

    function frameLoaded(frame){
       frame.contentWindow.print();
       document.getElementById('spanMess').style.display='none';",10000);
    }
    
    function PrintFrame(xFile){
       var holder = document.getElementById('iframePDFHolder');
       holder.innerHTML = '<iframe onload="frameLoaded(this)" src=" + xFile + '"></iframe>';
    }
    
    <div id="iframePDFHolder" style="display:none;"></div>
    
    <input type="image" onclick="PrintFrame('525.pdf')" src="../../cms_res/DisneyChannel/playhouse/images/buttons/printer.png" />
    <span id="spanMess" style="display:none;color:red;"><h3>Preparing Document For Print</h3></span>
    
        3
  •  0
  •   Tim Down    16 年前

    第一个问题是 parent 引用包含当前文档的文档的窗口对象,您实际上希望调用当前文档中的函数,因此 起源 不是必需的。

    第二,你应该使用 document.getElementById("iframePDF") 而不是依赖IE创建与页面中元素的ID相对应的全局对象属性这一令人讨厌的特性。要获取iframe的窗口对象,可以使用(非标准但广泛支持) contentWindow 财产。或者,指定 name 到您的iframe而不是ID并使用 window.frames["iframePDF"] .

    第三, new function() {...} 不是你想要的: new 没有必要,也没有混乱;放下它。

    第四,分配 onload iframe对象的属性不适用于所有浏览器。你需要处理 load 将文档中的事件加载到iframe并调用主文档中的函数,使用 起源 获取包含的文档。