How to print IFrame from javascript in Safari / Chrome

Can someone please help me print the contents of an IFrame via javascript call in Safari / Chrome.

This works in firefox:

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

this works in IE:

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

But I can't get something to work in Safari / Chrome.

thank

Andrew

+50
javascript printing iframe webkit
Jan 23 '09 at 13:53
source share
11 answers

Put the print function in the iframe and call it from the parent.

IFrame:

 function printMe() { window.print() } 

Parent:

 document.frame1.printMe() 
+42
Jan 23 '09 at 15:18
source share

Here is my complete cross-browser solution:

On the iframe page:

 function printPage() { print(); } 

On the home page

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

Update : Many people have problems with this in versions of IE released since I had this problem. I don’t have time to re-investigate this issue right now, but if you’re stuck, I suggest you read all the comments in this entire topic!

+48
Jan 23 '09 at 15:41
source share

I used the Andrew script, but added a fragment before calling the printPage () function. An IFrame needs focus, otherwise it will still print the parent frame in 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; } 

I do not thank me, but Andrew wrote this. I just made the setting = P

+32
May 26 '10 at 16:10
source share

In addition to the Andrew and Max solutions, using iframe.focus () appeared to print the parent frame instead of only printing the child iframe in IE8. Changing this line fixed this:

 function printIframe(id) { var iframe = document.frames ? document.frames[id] : document.getElementById(id); var ifWin = iframe.contentWindow || iframe; ifWin.focus(); ifWin.printPage(); return false; } 
+8
Apr 20 '11 at 15:52
source share

Use firefox window.frames , but also add the name property, as it uses an iframe in firefox

IE:

 window.frames[id] 

Firefox:

 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
Oct 28 2018-10-28
source share

It should be noted that if you test this locally using the file: ///, it will not work on chrome, since the function in the iframe will display as undefined. However, one day it will work on a web server.

+3
Dec 07 2018-10-12T00:
source share

I had to make a few changes to do this in IE8 (not tested with other IE accessories)

1) document.frames [param] seems to take a number, not an 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) I had a print dialog displayed when the page loads, and there was also a link to "Click here to start printing" (if it did not start automatically). To get it working, I had to add a focus () call

 <script type="text/javascript"> $(function(){ printPage(); }); function printPage() { focus(); print(); } </script> 
+3
Dec 07 2018-11-21T00:
source share

you can use

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

Work in Chrome!

0
Jan 21 '15 at 2:37
source share

You can also use

 top.iframeName.print(); 

or

 parent.iframeName.print(); 
0
Feb 12 '16 at
source share

"framePartsList.contentWindow.print ();" did not work in IE 11 ver11.0.43

So I used framePartsList.contentWindow.document.execCommand ('print', false, null);

0
Jun 22 '17 at 4:50
source share

Use this:

 window.onload = setTimeout("window.print()", 1000); 
-four
Nov 24 '10 at 15:35
source share



All Articles