There is a page for printing, before printing, I hide part of the elements, and part of it shows (using css);

After printing, this tab should close; I set the event handler <body onafterprint="self.close();"> to close only when I select print from the menu, and when I call window.print() in the code it does not close if I onafterprint event onafterprint to a separate function and I put the alert("...") before closing the window, the alert is displayed and after clicking on OK, the tab still does not close (I was in Chrome, Opera, FireFox ). Tell me, please, what could be wrong?

    1 answer 1

    First, the onafterprint attribute onafterprint not included in the specification, and therefore not supported by all browsers (only IE and FF). Secondly, those browsers that support it work with it differently (IE will call the function before printing, and FF - after closing the print dialog, even if the user has clicked Cancel). I would advise to do something like this:

     <html> <body onload="doPrint()"> <script type="text/javascript"> function doPrint() { window.print(); window.close(); } </script> </body> </html>