如何才能仅捕获打印页面中的取消事件?
How can I capture only the cancel event from the print page?
我有一个页面,单击按钮后,您会打开一个新选项卡,其中加载了打印内容,如果您单击取消"(不是打印),则必须调用一个方法.打印相同.
I have a page that on a button click you have a new tab opened with print loaded, there if you click cancel (not print) I have to call a method. Same for print.
如果要打印,则要执行PrintSuccesfully(),如果要取消打印,我要执行PrintCancelled().
In case of print a want to execute PrintSuccesfully(), if Cancel print I want to execute PrintCancelled().
打开新标签的代码:
var windowObject = window.open('', '_blank'); windowObject.document.write(generatePrintHTMLBody(scope)); windowObject.document.close(); windowObject.focus(); windowObject.print(); windowObject.close();我在beforeprint和onafterprint上进行了查找,但是都使用matchMedia,在CTRL + P事件上或在单击打印/取消"按钮时在我的代码上执行.
I looked up onbeforeprint and onafterprint, but both are executed with matchMedia, on CTRL+P event or on my code at Print / Cancel button click.
我测试的代码包含在这里:
(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; }());推荐答案
经过大量研究,您似乎无法执行此操作……每个浏览器上按钮的行为不同,或者具有相同的签名,因此没有解决方案存在.
After a lot of research it seems you cannot do this... The behaviour si diferent for the buttons on each browser or it has the same signature, so no resolution exists.
更多推荐
捕获(不同)打印/取消打印按钮事件(单击)
发布评论