捕捉(不同)打印/取消打印按钮事件(点击)
Capture (distinct) Print / Cancel Print Button Event (click)
如何才能只从打印页面捕获取消事件?
我有一个页面,点击一个按钮就会打开一个新标签页并加载打印,如果你点击取消(不打印)我必须调用一个方法。打印相同。
如果打印要执行PrintSuccesfully(),如果取消打印要执行PrintCancelled()。
我打开新标签页的代码:
var windowObject = window.open('', '_blank');
windowObject.document.write(generatePrintHTMLBody(scope));
windowObject.document.close();
windowObject.focus();
windowObject.print();
windowObject.close();
我查看了 onbeforeprint 和 onafterprint,但两者都是使用 matchMedia 在 CTRL+P 事件中或在我的代码中单击“打印/取消”按钮时执行的。
我测试的代码包含在发现的 generatePrintHTMLBody(scope) 中 here:
(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;
}());
经过大量研究,您似乎无法做到这一点...每个浏览器上的按钮行为不同,或者具有相同的签名,因此不存在解决方案。
如何才能只从打印页面捕获取消事件?
我有一个页面,点击一个按钮就会打开一个新标签页并加载打印,如果你点击取消(不打印)我必须调用一个方法。打印相同。
如果打印要执行PrintSuccesfully(),如果取消打印要执行PrintCancelled()。
我打开新标签页的代码:
var windowObject = window.open('', '_blank');
windowObject.document.write(generatePrintHTMLBody(scope));
windowObject.document.close();
windowObject.focus();
windowObject.print();
windowObject.close();
我查看了 onbeforeprint 和 onafterprint,但两者都是使用 matchMedia 在 CTRL+P 事件中或在我的代码中单击“打印/取消”按钮时执行的。
我测试的代码包含在发现的 generatePrintHTMLBody(scope) 中 here:
(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;
}());
经过大量研究,您似乎无法做到这一点...每个浏览器上的按钮行为不同,或者具有相同的签名,因此不存在解决方案。