问题
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.
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);
};
On the more printerfriendly page I create my layout and then call:
window.print();
By creating and appending an iframe, the user stays on the page in the browser, but the print layout is different.
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 ?
thanks,
Thomas
回答1:
No it is not possible to cancle the print event using Javascript.
Read this: [MDN, beforeprint - Cancelable:NO]
Also see the fiddle:
var beforePrint = function() {
console.log('Functionality to run before printing.');
alert('cancelable : '+event.cancelable)
};
来源:https://stackoverflow.com/questions/28937478/is-it-possible-to-cancel-print-in-javascript