Close page when printing

Asked

Viewed 2,130 times

3

I have the following situation: When confirming the opening of a call, in the confirmation directs to the printing page. When clicking, it already displays the print option, thus:

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

I wonder if there’s a way to get the window back to the previous page when starting the print. For example, go back to index.php after closing the print window.
Thank you

  • But when you click cancel or close after printing it doesn’t come back? I don’t understand.

  • It opens the print window, when I select the printer and print it closes the window, exactly as I needed.

3 answers

5

Simply add the following codes:

<script>
    window.print();
    window.addEventListener("afterprint", function(event) { window.close(); });
    window.onafterprint();
</script>

3


For the record, I decided as follows:

<body onload="ClosePrint()">

Function:

function ClosePrint() {
      setTimeout(function () { window.print(); }, 500);
      window.onfocus = function () { setTimeout(function () { window.close(); }, 500); }
}

3

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

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

However, as pointed out in Joe’s call ( https://stackoverflow.com/a/9920784/578667 ), This is not exactly how it is implemented in all cases. In most cases, both events trigger before the dialog; in others, the script execution may be interrupted during the print dialog, so both events can trigger at the same time (after the dialog has been completed).

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

https://developer.mozilla.org/en-US/docs/DOM/window.onbeforeprint

https://developer.mozilla.org/en-US/docs/DOM/window.onafterprint

The short answer: if you’re waiting to interfere with the print flow, they don’t. If you are waiting to trigger the code after printing, it will not work as you want; expect poor browser support, and try to degrade normally.

Source : https://stackoverflow.com/questions/11138939/how-to-trigger-javascript-on-print-event

  • Apart from the poor translation, I don’t agree with your answer. If you check here http://stackoverflow.com/questions/6460630/close-window-automatically-after-printing-dialog-closes the method I used works IN ANY BROWSER. The way I needed it, printing and closing, it worked properly.

  • Another, I never used window.onbeforeprint and window.onafterprint. I don’t know why that answer, if it has nothing to do with the solution.

  • Ok, a solution with setTimeout works. Is it the best way ? kkkkkkkkkkkkk

Browser other questions tagged

You are not signed in. Login or sign up in order to post.