2009-01-23 39 views
50

有人可以帮助我在Safari/Chrome中通过javascript调用打印IFrame的内容。如何在Safari/Chrome中打印来自JavaScript的IFrame

这在Firefox:

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

这部作品在IE:

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

但我不能得到任何Safari /铬工作。

感谢

安德鲁

+0

问候Andrew,有一个问题......它会自动在IE,Firefox中为PDF文件显示一个保存对话框。我如何抑制这一点。我尝试使用JavaScript设置iframe的src,但它仍然显示保存对话框 – 2011-12-04 09:42:04

回答

42

把打印功能在iframe和从父调用它。

的iframe:

function printMe() { 
    window.print() 
} 

父:

document.frame1.printMe() 
+6

您是如何获得document.frame1的?我不得不用这个: document.getElementById('frame1')。contentWindow.printMe() – 2009-06-09 16:44:49

+2

使用NAME属性,而不是ID。 – 2009-06-09 17:46:15

+2

它在IE8中不能正常工作。它正在打印所有内容(iframe之外)。请任何想法.. – DonX 2010-02-08 07:09:02

48

这里是我完整的,跨浏览器的解决方案:

在iframe页面

function printPage() { print(); } 

页面

function printIframe(id) 
{ 
    var iframe = document.frames ? document.frames[id] : document.getElementById(id); 
    var ifWin = iframe.contentWindow || iframe; 
    iframe.focus(); 
    ifWin.printPage(); 
    return false; 
} 

更新:许多人似乎是一种在,因为我有这个问题发布了IE的版本与此问题。我没有时间 - 现在重新调查,如果你卡住了,我建议你阅读这整个线索中的所有评论!

32

我使用了Andrew的脚本,但在printPage()函数被调用之前添加了一块。 iframe需要焦点,否则它仍然会在IE中打印父框架。

function printIframe(id) 
{ 
    var iframe = document.frames ? document.frames[id] : document.getElementById(id); 
    var ifWin = iframe.contentWindow || iframe; 
    iframe.focus(); 
    ifWin.printPage(); 
    return false; 
} 

不过谢谢你,这是安德鲁写的。我只是做了一个调整= P

4

使用Firefox window.frames也加入name属性,因为使用Firefox中的iframe中

IE:

window.frames[id] 

火狐:

window.frames[name] 

<img src="print.gif" onClick="javascript: window.frames['factura'].focus(); parent['factura'].print();"> 
<iframe src="factura.html" width="100%" height="400" id="factura" name="factura"></iframe> 
-4

使用这个:

window.onload = setTimeout("window.print()", 1000); 
3

有一点需要注意的是,如果您使用file:///在本地进行测试,它将无法在chrome上工作,因为iframe中的函数将显示为未定义。但是,一旦在Web服务器上,它将工作。

8

除了Andrew和Max的解决方案,使用iframe.focus()导致打印父框架,而不是在IE8中只打印子iframe。改变该行固定它:

function printIframe(id) 
{ 
    var iframe = document.frames ? document.frames[id] : document.getElementById(id); 
    var ifWin = iframe.contentWindow || iframe; 
    ifWin.focus(); 
    ifWin.printPage(); 
    return false; 
} 
3

我不得不做出一些修改,以便在IE8(不与其他IE口味测试)

1)document.frames [参数]似乎使它接受数,而不是ID

printIframe(0, 'print'); 

function printIframe(num, id) 
{ 
    var iframe = document.frames ? document.frames[num] : document.getElementById(id); 
    var ifWin = iframe.contentWindow || iframe; 

    ifWin.focus(); 
    ifWin.printPage(); 

    return false; 
} 

2)我必须在网页加载时显示的打印对话框,也有一个链接“点击这里开始打印”(如果它没有自动启动)。为了得到它的工作我不得不添加对焦()调用

<script type="text/javascript"> 
    $(function(){ 
    printPage(); 
    }); 

    function printPage() 
    { 
    focus(); 
    print(); 
    } 
</script> 
0

您可以在Chrome中使用

parent.frames['id'].print(); 

工作!

0

您还可以使用

top.iframeName.print(); 

parent.iframeName.print(); 
0

的 'framePartsList.contentWindow.print();'不工作在IE 11 ver11.0.43

因此我用 framePartsList.contentWindow.document.execCommand('print',false,null);