parent.iframePDF未定义

时间:2010-08-24 09:01:28

标签: javascript

我得到'parent.iframePDF未定义'当我在Firefox中使用以下代码并且IE中出现'对象预期'错误时出现JavaScript错误。

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

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 个答案:

答案 0 :(得分:1)

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

如果您使用document.getElementById('iframePDF'),那么您就不能使用.location.href。

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

答案 1 :(得分:0)

您的代码可能无法在其他浏览器上成功调用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>

答案 2 :(得分:0)

第一个问题是parent引用包含当前文档的文档的窗口对象,并且您实际上想要调用当前文档中的函数,因此不需要parent

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

第三,new function() {...}不是你想要的:new没有必要和混乱;放弃那个。

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