Andrew Bullock
Andrew Bullock

Reputation: 37378

How do I print an IFrame from javascript in Safari/Chrome

Can someone please help me out with printing the contents of an IFrame via a 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 anything to work in Safari/Chrome.

Thanks

Andrew

Upvotes: 52

Views: 143269

Answers (12)

Elijah M
Elijah M

Reputation: 835

In Chrome:

  1. Press Ctrl+Shift+C to select the iframe.
  2. Click anywhere in the iframe.
  3. Go to the console tab and type window.print();

This works because in Chrome Dev Tools, the window element adjusts to whatever <html> context you are in.

Upvotes: 0

Andrew Bullock
Andrew Bullock

Reputation: 37378

Here is my complete, cross browser solution:

In the iframe page:

function printPage() { print(); }

In the main 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 seem to be having problems with this in versions of IE released since I had this problem. I do not have the time to re-investigate this right now, but, if you are stuck I suggest you read all the comments in this entire thread!

Upvotes: 49

Imran Khan Hunzai
Imran Khan Hunzai

Reputation: 310

The 'framePartsList.contentWindow.print();' was not working in IE 11 ver11.0.43

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

Upvotes: 0

LENG UNG
LENG UNG

Reputation: 473

You can also use

top.iframeName.print();

or

parent.iframeName.print();

Upvotes: 0

Rogerio de Moraes
Rogerio de Moraes

Reputation: 1577

You can use

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

Work at Chrome!

Upvotes: 0

Luis Garcia
Luis Garcia

Reputation: 49

Use firefox window.frames but also add the name property because that uses the 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>

Upvotes: 4

Yura Omelchuk
Yura Omelchuk

Reputation: 391

I had to make few modifications in order to make it with in IE8 (didn't test with other IE flavours)

1) document.frames[param] seem to accept a number, not 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 upon page load and also there was a link to "Click here to start printing" (if it didn't start automatically). In order to get it work I had to add focus() call

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

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

Upvotes: 4

YTmGeorge
YTmGeorge

Reputation: 1

Use this:

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

Upvotes: -4

serdar
serdar

Reputation: 560

In addition to Andrew's and Max's solutions, using iframe.focus() resulted in printing parent frame instead of printing only child iframe in IE8. Changing that line fixed it:

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

Upvotes: 8

Blake Hancock
Blake Hancock

Reputation: 41

One thing to note is if you are testing this locally using file:///, it will not work on chrome as the function in the iframe will appear as undefined. However once on a web server it will work.

Upvotes: 3

M.W. Felker
M.W. Felker

Reputation: 4823

I used Andrew's script but added a piece before the printPage() function is called. The 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;
}

Don't thank me though, it was Andrew who wrote this. I just made a tweak =P

Upvotes: 34

Diodeus - James MacFarlane
Diodeus - James MacFarlane

Reputation: 114347

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

iframe:

function printMe() {
  window.print()
}

parent:

document.frame1.printMe()

Upvotes: 42

Related Questions