Firefox可以'无法访问同一域上的iframe打印

Firefox can't access iframe print on the same domain

本文关键字:打印 iframe 访问 可以 Firefox      更新时间:2023-09-26

我的页面上有一个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();

解决方法是使用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>

有关

的讨论,请参阅此内容