【发布时间】:2015-06-16 08:52:57
【问题描述】:
如何只从打印页面捕获取消事件?
我有一个页面,在按钮上单击您会打开一个新选项卡并加载打印,如果您单击取消(不是打印),我必须调用一个方法。打印也一样。
如果打印想执行 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 事件或我的代码在打印/取消按钮单击时执行的。
我测试的代码包含在here找到的 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;
}());
【问题讨论】:
标签: javascript events button printing click