是否可以取消JavaScript中的打印 [英] Is it possible to cancel print in javascript
问题描述
我正在使用以下代码:检测浏览器打印事件以检测用户是否想要打印页面.到目前为止,它可以按预期工作.
I am using the code found here: Detecting browser print event to detect if the user wants to print the page. So far it works as intended.
在"afterPrint"功能中,我调用了一个函数,该函数创建并附加一个iframe,该iframe包含另一个页面,该页面具有需要打印的内容的不同且更加打印友好的布局:
In the "afterPrint"-function I call a function which creates and appends an iframe containing another page with a different, and much more printfriendly layout of what needs to be printed:
function printIframe (printsrc) {
var iframe = $('iframe#print');
if (iframe.length == 0)
iframe = $('<iframe id="print" />').attr('src', printsrc).css("width", "0px").css("height", "0px").css("position", "absolute").css("left", "-9999px").appendTo($("body:first"));
else
iframe.attr('src', printsrc);
};
在更便于打印的页面上,我创建布局,然后调用: window.print();
On the more printerfriendly page I create my layout and then call:
window.print();
通过创建和附加iframe,用户将停留在浏览器中的页面上,但是打印布局有所不同.
By creating and appending an iframe, the user stays on the page in the browser, but the print layout is different.
似乎要加载对打印机友好的页面,唯一的方法是在打印对话框中取消打印,然后触发afterPrint功能.是否可以通过javascript取消用户的打印对话框?
It seems the only way to get the printerfriendly page to load is by cancelling the print in the print dialog, which then triggers the afterPrint-function. Is it somehow possible to cancel the user's print dialog in javascript ?
谢谢
托马斯
推荐答案
否,无法使用Java取消打印事件.
No it is not possible to cancle the print event using Javascript.
阅读此内容: [MDN,预印本-可取消:否]
Read this: [MDN, beforeprint - Cancelable:NO]
另请参见小提琴:
var beforePrint = function() {
console.log('Functionality to run before printing.');
alert('cancelable : '+event.cancelable)
};
这篇关于是否可以取消JavaScript中的打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!