【问题标题】:Firefox can't access iframe print on the same domainFirefox 无法访问同一域上的 iframe 打印
【发布时间】:2013-08-02 02:56:19
【问题描述】:

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

错误:访问属性“打印”的权限被拒绝

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

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

我的打印代码:

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

【问题讨论】:

    标签: javascript firefox iframe printing


    【解决方案1】:

    解决此问题的方法是使用 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-31
      • 2015-12-25
      • 2013-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-29
      相关资源
      最近更新 更多