title: Print to client side on window.load
body: <p>I'm trying to print to the client's printer immediately upon a page loading, using the following:</p>
  
  <pre><code>function openWin() {
      var printWindow = window.open();
      printWindow.document.write('&lt;html&gt;&lt;head&gt;&lt;title&gt;Test Page&lt;/title&gt;');
      printWindow.document.write('&lt;/head&gt;&lt;body&gt;');
      printWindow.document.write("Name: &lt;?php echo $firstname . " " . $lastname; ?&gt;&lt;br /&gt;Spouse: &lt;?php echo $sfirstname . " " . $slastname; ?&gt;&lt;br /&gt;Address: &lt;?php echo $_POST['address'] ?&gt;&lt;br /&gt;City/State/Zip:  &lt;?php echo $_POST['city'] . ", " . $_POST['state'] . " " . $_POST['zip'] ?&gt;&lt;br /&gt;");  
      printWindow.document.write('&lt;/body&gt;&lt;/html&gt;');
      printWindow.document.close();
      printWindow.focus();
      printWindow.print();
      printWindow.close(); 
  }
  </code></pre>
  
  <p>To get the function to fire, I'm using</p>
  
  <pre><code>$(window).load(function(){
      openWin();
  });
  </code></pre>
  
  <p>When the document opens, nothing prints - i.e., the <code>openWin()</code> function doesn't fire.  I've tried creating a button for it and using trigger to click the button when the window loads. I've also tried <code>setTimeout()</code> to delay a while so the page can continue loading, etc.   Nothing works. </p>
  
  <p>I get this error at the console:</p>
  
  <blockquote>
    <p>application2.php:240 Uncaught TypeError: Cannot read property 'document' of null</p>
  </blockquote>
  
  <p>BUT, when I simply enter <code>openWin()</code> at the console, the print page opens properly.</p>
  
  <p>Any help would be appreciated.</p>