問題描述
Firefox 無法訪問同一域上的 iframe 打印 (Firefox can't access iframe print on the same domain)
I have an iframe on my page that displays a PDF that is located on the same domain. Because of how this system is built, I am required to use full paths in my src tags (e.g http://www.example.com/test.pdf). When I try to print i get the following error:
Error: Permission denied to access property 'print'
If I remove "http://www.example.com/", Firefox is able to print, but that messes up other parts of the system.
So it seems that Firefox thinks that the iframe src is on a different domain just because i use full paths, but it isn't. Is there a workaround for this?
My print code:
$('#iframe')[0].focus();
$('#iframe')[0].contentWindow.print();
參考解法
方法 1:
A work around for this would be to use css @media. Refer an example below,
<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
(by Oskar Pålsson、Dileepa)