Chrome window.print() window.close() results in 'print preview failed'. Solution?
I don't think there are any standardised print events. IE has a couple, but I realise that doesn't help you for Chrome.
I think you might be left with only two options. A manual close button or some form of delay using setTimeout
.
I just managed to come up with a solution that fits me. I started out with the reply from Peru but didn't want to use jQuery in this solution.
window.onload = function () {
window.print();
setTimeout(function(){window.close();}, 1);
}
For some reason Chrome dont start the timeout timer until the print dialog is closed.
This question is a top hit on Google so I thought I would add what I found, even though it does not exactly mirror your situation. If you have a link that calls window.print()
then its onclick handler must return false or you get the error. This is true even if the link is a hash and goes nowhere!
Print is unavailable because the page you were trying to print has been closed
To fix this make sure you add return false to the link.
<a href="#" onclick="window.print(); return false;" >Print</a>
Here is the Chromium bug that addresses this. It is marked as fixed for Chrome 17 (not yet released) and I have verified the fix in Chrome 18.
http://code.google.com/p/chromium/issues/detail?id=92107