Close page when printing

0

I have the following situation: When you confirm the opening of a call, the confirmation directs you to the print page. When loading, it already displays the option to print, like this:

<body onload="window.print();">

I wonder if you have the window back to the previous page when you start printing. For example, return to index.php after closing the print window. Thanks

    
asked by anonymous 14.04.2016 / 16:43

2 answers

0

To be registered, I have decided as follows:

<body onload="ClosePrint()">

Function:

function ClosePrint() {
      setTimeout(function () { window.print(); }, 500);
      window.onfocus = function () { setTimeout(function () { window.close(); }, 500); }
}
    
14.04.2016 / 16:49
0

For anyone to stumble upon this answer from Google, let me try to clarify things:

As Ajay pointed out, there are two events that are triggered for printing, but they are not well supported; as far as I read, they are only supported in (6+) browsers Internet Explorer and Firefox. These events are window.onbeforeprint and window.onafterprint, which (as expected) will be fired before and after the print job.

However, as pointed out in Joe's link ( link ), this is not exactly how it is implemented in all the cases. In most cases, both events fire before the dialogue; in others, the execution of the script may be interrupted during the print dialog, so that both events can fire at the same time (after the dialog has completed).

For more information (and browser support) for these two events:

link

link

The short answer: If you're hoping to interfere with the print flow, do not do it. If you are hoping to trigger the code after printing, it will not work as you are wanting; wait for the poor browser support, and try to degrade normally.

Source: link

    
14.04.2016 / 17:54