【问题标题】:Capture (distinct) Print / Cancel Print Button Event (click)捕获(不同的)打印/取消打印按钮事件(单击)
【发布时间】:2015-06-16 08:52:57
【问题描述】:

如何只从打印页面捕获取消事件?

我有一个页面,在按钮上单击您会打开一个新选项卡并加载打印,如果您单击取消(不是打印),我必须调用一个方法。打印也一样。

如果打印想执行 PrintSuccesfully(),如果取消打印我想执行 PrintCancelled()。

我打开新标签的代码:

var windowObject = window.open('', '_blank');
windowObject.document.write(generatePrintHTMLBody(scope));
windowObject.document.close();
windowObject.focus();
windowObject.print();
windowObject.close();

我查找了 onbeforeprint 和 onafterprint,但两者都是使用 matchMedia、CTRL+P 事件或我的代码在打印/取消按钮单击时执行的。

我测试的代码包含在here找到的 generatePrintHTMLBody(scope) 中:

(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;
}());

【问题讨论】:

    标签: javascript events button printing click


    【解决方案1】:

    经过大量研究,您似乎无法做到这一点...每个浏览器上的按钮行为不同,或者具有相同的签名,因此不存在解决方案。

    【讨论】:

    • 我们都可以接受 ???快到 2021 年了,我不知道我的用户正在点击哪个按钮。我认为我们可以做得更好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-05
    • 1970-01-01
    • 1970-01-01
    • 2011-09-11
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    相关资源
    最近更新 更多