title: Remove iframe after printing
body: <p>I'm new to HTML and Javascript. I'm trying to write a Javascript function to print the content of an (hidden) iframe in order to print documents (to the user, seemingly) without opening them.</p>
  
  <p>I based the function on the example I found here: <a href="https://developer.mozilla.org/en-US/docs/Printing#Print_an_external_page_without_opening_it" rel="nofollow">https://developer.mozilla.org/en-US/docs/Printing#Print_an_external_page_without_opening_it</a></p>
  
  <p>Printing the content works fine but the trouble is removing the iframe from the document after the printing has finished. This is what my code looks like now.</p>
  
  <pre><code>function closePrint () {
              var element = document.getElementById("printFrame");
              element.parentNode.removeChild(element);
          }
  
          function setPrint () {
              this.contentWindow.__container__ = this;
              this.contentWindow.onbeforeunload = setTimeout(closePrint, 100);
              this.contentWindow.onafterprint = setTimeout(closePrint, 100);
  
              this.contentWindow.focus(); // Required for IE
              this.contentWindow.print();
  
          }
  
          function printPage (sURL) {
              var oHiddFrame = document.createElement("iframe");
              oHiddFrame.onload = setPrint;
              oHiddFrame.width = 0;
              oHiddFrame.height = 0;
              oHiddFrame.style.position = "fixed";
              oHiddFrame.style.right = "0";
              oHiddFrame.style.bottom = "0";
              oHiddFrame.id = "printFrame";
              oHiddFrame.src = sURL;
              document.body.appendChild(oHiddFrame);
          }
  </code></pre>
  
  <p>I changed two lines in the example from</p>
  
  <pre><code>            this.contentWindow.onbeforeunload = closePrint;
              this.contentWindow.onafterprint = closePrint;
  </code></pre>
  
  <p>to </p>
  
  <pre><code>            this.contentWindow.onbeforeunload = setTimeout(closePrint, 100);
              this.contentWindow.onafterprint = setTimeout(closePrint, 100);
  </code></pre>
  
  <p>As it didn't remove the iframes without the timeout.
  This works fine in both IE11 and Chrome, but in IE compitability mode (which I think emulates IE7) it gives me an error "Not implemented" when I try to use setTimeout.</p>
  
  <p>So my question is, is there another way to run the closePrint function after a timeout or some other way to remove the iframe from the document when I've printed the content? Any help is appreciated.</p>