1

I pulled this code from this page on MDN. It works wonderfully, except for IE10 (and others, likely).

The purpose of this code is to open a URL other than the current page in an iframe and print it. In IE10, it just prints the current page.

function closePrint () {
  document.body.removeChild(this.__container__);
}

function setPrint () {
  this.contentWindow.__container__ = this;
  this.contentWindow.onbeforeunload = closePrint;
  this.contentWindow.onafterprint = closePrint;
  this.contentWindow.focus(); // Required for IE
  this.contentWindow.print();
}

function printPage (sURL) {
  var oHiddFrame = document.createElement("iframe");
  oHiddFrame.onload = setPrint;
  oHiddFrame.style.visibility = "hidden";
  oHiddFrame.style.position = "fixed";
  oHiddFrame.style.right = "0";
  oHiddFrame.style.bottom = "0";
  oHiddFrame.src = sURL;
  document.body.appendChild(oHiddFrame);
}

My ability to debug this has been limited due to not having a Windows VM. Does anything look obviously wrong here?

Eric
  • 999
  • 2
  • 8
  • 23
  • 1
    http://stackoverflow.com/questions/17573733/unable-to-print-an-iframe-on-ie-using-javascript-prints-parent-page-instead – adeneo May 13 '14 at 22:58
  • Thanks, this is helpful, but I still don't understand _why_ this fails. – Eric May 14 '14 at 00:07

0 Answers0