I have a print page that opens in a new tab or window. The resulting page opens a print dialog. After the user makes a selection on the print dialog, the page then closes the tab/window.
window.print();
window.close();
This used to work great in the major browsers, but one of the latest versions of Chrome breaks this (i.e. 14.0.835.202).
I receive the following message from what I guess is the chrome print plugin: "Print preview failed".
Does anyone have a solution to close the Chrome tab/window after printing?
All solution above doesn't solved my problem, using this code solved my problem
It work fine for me...
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!To fix this make sure you add return false to the link.
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
My solution to this:
EDIT: Printing on Chrome version >= 17 works just like IE/FF, so make sure that you account for that as well
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
.After an afternoon of research and thanks to @SpYk3HH's answer, I've found a solution that works in the versions I am running currently:
IE : 9.0.8...
FireFox : 15.01
Chrome : 23.0.1271.97 m
Safari : 5.1.7
When preparing the html to be displayed on a print specific page, include a check for
and then notify the user to enable popups temporarily. In order to allow your page to open, display the print dialog and close upon print dialog close add this function to the onload function for the window's body:
The reason this works is because fundamentally we are still dealing with objects and events. The browsers may behave differently in some things, but they all have common ground to look for. A simple onmouseover event will hopefully always happen when a window gains focus, and so this code will hopefully not lose its relevance.