Javascript仅打印iframe内容

这是我的代码

<script> var body = "dddddd" var script = "<script>window.print();</scr'+'ipt>"; var newWin = $("#printf")[0].contentWindow.document; newWin.open(); newWin.close(); $("body",newWin).append(body+script); </script> <iframe id="printf"></iframe> 

这工作,但它打印父页面,我怎么才能打印只是iframe?

我不希望这个工作

反而尝试

 window.frames["printf"].focus(); window.frames["printf"].print(); 

并使用

 <iframe id="printf" name="printf"></iframe> 

或者尝试旧的

 var newWin = window.frames["printf"]; newWin.document.write('<body onload="window.print()">dddd</body>'); newWin.document.close(); 

如果jQuery不能破解它

现场演示

 document.getElementById("printf").contentWindow.print()​​​​​​; 

同样的来源政策适用。

简单的方法(ie7 +,Firefox,Chrome,safaritesting)就是这样

 //id is the id of the iframe function printFrame(id) { var frm = document.getElementById(id).contentWindow; frm.focus();// focus on contentWindow is needed on some ie versions frm.print(); return false; } 

一个备用的select,这可能是也可能不适合,但如果是清洁的话:

如果您始终只想从页面打印iframe,则可以使用单独的“@media print {}”样式表来隐藏iframe之外的所有内容。 那么你可以正常打印页面。

我在IE8中遇到了上述所有解决scheme的问题,已经find了一个在IE 8 + 9,Chrome,Safari和Firefox中testing过的体面的解决方法。 对于我的情况,我需要打印一个dynamic生成的报告:

 // create content of iframe var content = '<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">'+ '<head><link href="/css/print.css" media="all" rel="stylesheet" type="text/css"></head>'+ '<body>(rest of body content)'+ '<script type="text/javascript">function printPage() { window.focus(); window.print();return; }</script>'+ '</body></html>'; 

请注意正文closures标记之前的printPage()javascript方法。

接下来创buildiframe并将其附加到父主体,以使其contentWindow可用:

 var newIframe = document.createElement('iframe'); newIframe.width = '0'; newIframe.height = '0'; newIframe.src = 'about:blank'; document.body.appendChild(newIframe); 

接下来设置内容:

 newIframe.contentWindow.contents = content; newIframe.src = 'javascript:window["contents"]'; 

在这里,我们将dynamic内容variables设置为iframe的窗口对象,然后通过javascript:scheme调用它。

最后打印; 集中iframe并在iframe内容中调用javascript printPage()函数:

 newIframe.focus(); setTimeout(function() { newIframe.contentWindow.printPage(); }, 200); return; 

setTimeout不一定需要,但是如果你加载大量的内容,我发现Chrome浏览器偶尔打印没有它,所以这一步被推荐。 另一种方法是将'newIframe.contentWindow.printPage();' 在try catch中,将setTimeout包装的版本放在catch块中。

希望这可以帮助别人,因为我花了很多时间find一个在多个浏览器上运行良好的解决scheme。 感谢SpareCycles 。

编辑:

而不是使用setTimeout来调用printPage函数使用以下内容:

 newIframe.onload = function() { newIframe.contentWindow.printPage(); } 

你可以使用这个命令:

 document.getElementById('iframeid').contentWindow.print(); 

这个命令基本上和window.print()是一样的,但是当我们想要打印的窗口在iframe中时,我们首先需要获得该窗口的一个实例作为javascript对象。

所以,参考iframe,我们首先使用它的id来获取iframe,然后它的contentWindow返回一个窗口(DOM)对象。 所以,我们可以直接在这个对象上使用window.print()函数。

此时,iframe中不需要脚本标签。 这适用于我(在Chrome,Firefox,IE11和node-webkit 0.12testing):

 <script> window.onload = function() { var body = 'dddddd'; var newWin = document.getElementById('printf').contentWindow; newWin.document.write(body); newWin.document.close(); //important! newWin.focus(); //IE fix newWin.print(); } </script> <iframe id="printf"></iframe> 

感谢所有的答案,保存我的一天。

如果使用javascript document.write()设置IFrame的内容,则必须通过newWin.document.close();closures文档newWin.document.close(); 否则以下代码将不起作用,打印将打印整个页面的内容而不是仅IFrame的内容。

 var frm = document.getElementById(id).contentWindow; frm.focus();// focus on contentWindow is needed on some ie versions frm.print(); 

我想知道你做iframe打印的目的是什么。

我刚才遇到了一个类似的问题:使用chrome的打印预览来生成一个iframe的PDF文件。

最后我用一个技巧解决了我的问题:

 $('#print').click(function() { $('#noniframe').hide(); // hide other elements window.print(); // now, only the iframe left $('#noniframe').show(); // show other elements again. }); 

使用此代码的IE9及以上:

 window.frames["printf"].focus(); window.frames["printf"].print(); 

对于IE8:

 window.frames[0].focus(); window.frames[0].print();