有没有办法捕获浏览器的打印事件并取消打印对话的外观?
示例:
User clicks in "File" -> "Print".
Before the print dialog appears, show a confirm() like 'It is possible that not all data is printed, continue anyway? <accept> <cancel>
User clicks <accept> -> Print dialog appears
User clicks <cancel> -> Print dialog doesn't appear
现在我有这个:
var beforePrint = function(ev) {
if (!messageShown) {
var result = confirm('Are you sure?');
if (!result) {
// TODO: Cancel
}
}
};
var afterPrint = function(ev) {
// TODO
};
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)
您无需取消它。如果点击“接受”,只需致电window.print()
即可。如果他们使用cntrl-p或file-&gt;打印,无论如何都是以编程方式控制的。顺便说一句,应该注意的是,你实际上无法阻止用户手动尝试打印。这包括拦截他们产生对话框的尝试并阻止它显示。您可以做的最好的事情是默认情况下将样式表应用于打印,然后动态更改该打印样式表,以便在内容使用代码进行打印时启用内容。