I'm trying to print to the client's printer immediately upon a page loading, using the following:

```
function openWin() {
    var printWindow = window.open();
    printWindow.document.write('<html><head><title>Test Page</title>');
    printWindow.document.write('</head><body>');
    printWindow.document.write("Name: <?php echo $firstname . " " . $lastname; ?><br />Spouse: <?php echo $sfirstname . " " . $slastname; ?><br />Address: <?php echo $_POST['address'] ?><br />City/State/Zip:  <?php echo $_POST['city'] . ", " . $_POST['state'] . " " . $_POST['zip'] ?><br />");  
    printWindow.document.write('</body></html>');
    printWindow.document.close();
    printWindow.focus();
    printWindow.print();
    printWindow.close(); 
}

```

To get the function to fire, I'm using

```
$(window).load(function(){
    openWin();
});

```

When the document opens, nothing prints - i.e., the `openWin()` 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 `setTimeout()` to delay a while so the page can continue loading, etc. Nothing works. 

I get this error at the console:

> 
> application2.php:240 Uncaught TypeError: Cannot read property 'document' of null
> 
> 
> 

BUT, when I simply enter `openWin()` at the console, the print page opens properly.

Any help would be appreciated.