window.print()函数弹出打印屏幕,但我想在弹出window.print myFunction()之前完成它的过程。所以我的程序等待完成myFunction。
(function () {
var beforePrint = function () {
console.log('Functionality to run after printing');
myFunction();
//window.print() pop up should wait until myFunction() finish
};
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;
}());
答案 0 :(得分:0)
您可以使用jquerys deferred / then功能在打印前执行操作。 像这样:
$(function() {
var filterResolve = function() {
var defer = $.Deferred(),
filtered = defer.then(function(value) {
//do lengthy stuff here
alert('Before Print');
return true;
//or false if you don't want to print
});
defer.resolve();
filtered.done(function(value) {
if (value == true) {
window.print();
//here you can log that the print dialog was shown to the user
//but not if he hit OK or Cancel
}
});
};
$("button").on("click", filterResolve);
});
但据我所知,(在任何浏览器中)都无法检测是否打印了任何内容。即使用户在打印对话框中单击“确定”或“取消”也不行。