Firefox无法访问同一域中的iframe打印

时间:2013-08-01 09:10:25

标签: javascript firefox iframe printing

我的页面上有一个iframe,显示位于同一域中的PDF。由于这个系统是如何构建的,我需要在我的src标签中使用完整路径(例如http://www.example.com/test.pdf)。当我尝试打印时,我收到以下错误:

  

错误:访问属性'print'的权限被拒绝

如果删除“http://www.example.com/”,Firefox就可以打印,但这会混淆系统的其他部分。

所以似乎Firefox认为iframe src只是因为我使用完整路径而在不同的域上,但事实并非如此。有解决方法吗?

我的打印代码:

$('#iframe')[0].focus();
$('#iframe')[0].contentWindow.print();

1 个答案:

答案 0 :(得分:2)

解决这个问题的方法是使用css @media。请参考下面的示例,

<BODY>
<STYLE type="text/css">
@media print 
{
  .dontprint{display:none} 
}
</STYLE>
<SCRIPT type="text/javascript">
function printPdf(){
        window.frames["printf"].focus();
        try {
            window.frames["printf"].print();
        }
        catch(e){
            window.print();
            console.log(e);
        }
    }
</SCRIPT>

<DIV class="dontprint" >
Some of your content here
<form><input type="button" onClick="printPdf()" value="Print"/></form>
...
...
</div>
<IFrame id="printf" src="whatever"></IFRAME>
<DIV class="dontprint" >
more content
...
...

</div>
</BODY>

Refer this for discussion