如何检测window.print()完成 [英] how to detect window.print() finish
问题描述
在我的应用程序中,我尝试打印出用户的凭证页面,我用过:
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
'是 div
在我的页面中存储有关优惠券的信息。
'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?
推荐答案
您可以通过FireFox和Internet Explorer监听后期打印事件。
In FireFox and Internet Explorer you can listen for the after print event.
https://developer.mozilla.org/en-US/docs/Web/API/window.onafterprint
window.onafterprint = function(){
console.log("Printing completed...");
}
可以使用window.matchMedia在其他浏览器中获取此功能。
It may be possible to use window.matchMedia to get this functiionality in other browsers.
(function() {
var beforePrint = function() {
console.log('Functionality to run before printing.');
};
var afterPrint = function() {
console.log('Functionality to run after printing');
};
if (window.matchMedia) {
var mediaQueryList = window.matchMedia('print');
mediaQueryList.addListener(function(mql) {
if (mql.matches) {
beforePrint();
} else {
afterPrint();
}
});
}
window.onbeforeprint = beforePrint;
window.onafterprint = afterPrint;
}());
资料来源: http://tjvantoll.com/2012/06/15/detecting-print-requests-with-javascript/
这篇关于如何检测window.print()完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!