如何仅从打印页面捕获取消事件?
我有一个页面,单击按钮时打开了一个加载打印的新选项卡,如果您单击取消(不打印),我必须调用一个方法。打印也一样。
在打印的情况下,想要执行打印成功(),如果取消打印,我想执行打印取消()。
打开新选项卡的代码:
var windowObject = window.open('', '_blank');
windowObject.document.write(generatePrintHTMLBody(scope));
windowObject.document.close();
windowObject.focus();
windowObject.print();
windowObject.close();
我查找了 onbeforeprint 和 onpostprint,但两者都是使用 matchMedia、CTRL+P 事件或我的代码在打印/取消按钮单击时执行的。
我测试的代码包含在 generatePrintHTMLBody(scope) 中,在这里找到:
(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;
}());
经过大量研究,您似乎无法做到这一点...每个浏览器上的按钮的行为都不同,或者它具有相同的签名,因此不存在分辨率。