In my application, I tried to print out a voucher page for user, I used:
var htm ="<div>Voucher Details</div>";
$('#divprint').html(htm);
window.setTimeout('window.print()',2000);
'divprint
' is a div
in my page which store information about the voucher.
It works, and the print page pops up. But I want to further proceed the application once user
click 'print
' or 'close
' the pop up window.
for example, I'd like to redirect user to another page after pop up window is closed:
window.application.directtoantherpage();//a function which direct user to other page
How to determine the pop up print window is closed or print is finished?
In FireFox and Internet Explorer you can listen for the after print event.
https://developer.mozilla.org/en-US/docs/Web/API/window.onafterprint
It may be possible to use window.matchMedia to get this functiionality in other browsers.
Source: http://tjvantoll.com/2012/06/15/detecting-print-requests-with-javascript/
See https://stackoverflow.com/a/15662720/687315. As a workaround, you can listen for the
afterPrint
event on the window (Firefox and IE) and listen for mouse movement on the document (indicating that the user has closed the print dialog and returned to the page) after thewindow.mediaMatch
API indicates that the media no longer matches "print" (Firefox and Chrome).Keep in mind that the user may or may not have actually printed the document. Also, if you call
window.print()
too often in Chrome, the user may not have even been prompted to print.Tested IE, FF, Chrome and works in all.
On Chrome 41.0.. I have to put this
I print automatically on load page:
JS:
It works too for IE 10
window.print behaves synchronously on chrome .. try this in your console
"printed" doesn't display unless the print dialog is closed(canceled/saved/printed) by the user.
Here is a more detailed explanation about this issue.
I am not sure about IE or Firefox will check and update that later
On chrome (V.35.0.1916.153 m) Try this:
Works great for me. It will close window after user finished working on printing dialog.