title: window.open with blank url wait for dynamic html to load then print
body: <pre><code>&lt;script&gt;
     var win = window.open();
     win.document.body.innerHTML = "this is html that I want to print";
     win.onload = function () { win.print(); }
  &lt;/script&gt;
  </code></pre>
  
  <p>everything works but print, how do I wait until the page loads the dynamic html with blank URL, then print?</p>
  
  <p>I don't think there is a way to do it, like the user below me the onload wont fire probably because as soon as the window opened the page was already loaded. What I'm trying to do is load the entire page FIRST then print. The script I posted doesn't do this.</p>
  
  <p>Martin, using your code:</p>
  
  <pre><code>&lt;script&gt;
  var win = window.open();
  win.document.body.innerHTML = "&lt;img src='https://upload.wikimedia.org/wikipedia/commons/2/24/Willaerts_Adam_The_Embarkation_of_the_Elector_Palantine_Oil_Canvas-huge.jpg'/&gt;";
  win.print();
  &lt;/script&gt;
  </code></pre>
  
  <p>In chrome it prints a blank page.</p>
  
  <p>EDIT:</p>
  
  <p>Martin, the onload img attribute did the trick.  Heres what I did to modify the script:</p>
  
  <pre><code>&lt;script&gt;
   var win = window.open();
   var html = "this is some test html &lt;br&gt;&lt;img src='https://upload.wikimedia.org/wikipedia/commons/2/24/Willaerts_Adam_The_Embarkation_of_the_Elector_Palantine_Oil_Canvas-huge.jpg'/&gt;";
   html = html.replace("&lt;img ","&lt;img onload='window.print();' ");
   win.document.body.innerHTML = html;
  &lt;/script&gt;
  </code></pre>
  
  <p>So all img tags will add a onload=window.print.  But really only one of them needs a onload (I'm pretty sure....). But to give you more information, in my scenario the variable html could be an infinite number of images, random text,tables, etc.  It could be anything.  Thank you Martin for having the patience to re-read this thread and reply.</p>