简体   繁体   English

检测用户是否通过javascript打印内容

[英]Detect if a user prints something via javascript

I've got a problem with detecting a print event in javascript. 我在JavaScript中检测到打印事件时遇到了问题。 For example, a user wats to print document and presses print in a web-page, then another window appers, eg to print from Adobe Reader, then appears another window of Adobe Reader where you can set properties, choose pages to print and what not...and there is the print button in this window. 例如,用户尝试打印文档并在网页中按打印,然后出现另一个窗口,例如从Adobe Reader打印,然后出现Adobe Reader的另一个窗口,您可以在其中设置属性,选择要打印的页面以及不打印什么...并且此窗口中有打印按钮。 Can I actually detect when the user presses this print button inside this Adobe Reader window in browser using javascript? 我实际上可以检测到用户何时使用JavaScript在浏览器的Adobe Reader窗口中按下此打印按钮吗?

I've already tried using onafterprint but maybe I didn't do this correctly or I'dont know. 我已经尝试过使用onafterprint但也许我没有正确执行此操作,或者我不知道。

it was something like this inside my main js file. 在我的主要js文件中是这样的。

window.onbeforeprint = function() {
    console.log('This will be called before the user prints.');
};
window.onafterprint = function() {
    console.log('This will be called after the user prints');   
};

I took that from here: https://www.tjvantoll.com/2012/06/15/detecting-print-requests-with-javascript/ 我是从这里拿来的: https//www.tjvantoll.com/2012/06/15/detecting-print-requests-with-javascript/

Do you realize that you code do nothing ? 您是否意识到您的代码什么都不做? This one will help you. 这将帮助您。

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

run it in your dev console on any open page, then hit ctrl-P and you should see message. 在任何打开的页面上的开发控制台中运行它,然后按ctrl-P,您应该会看到消息。

Have you tried using the matchMedia in that same link? 您是否尝试过在同一链接中使用matchMedia

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

Haven't done the full battery of browser compatibility checks, but that code works and prints both statements for me in Safari 10.1.1 whereas the onafterprint stuff doesn't work (though I exported as pdf once I was in the print dialog, since I don't have a printer). 尚未完成浏览器兼容性的全部检查,但是该代码可以正常工作并在Safari 10.1.1中为我打印这两个语句,而onafterprint内容不起作用(尽管我进入打印对话框后已导出为pdf,因为我没有打印机)。

I'm assume by Adobe Reader you just mean the normal print popup where you select a printer and number of copies/which pages/etc, since as far as I'm aware Adobe Reader is a desktop software 我认为Adobe Reader只是指正常的打印弹出式窗口,您可以在其中选择打印机以及份数/页数等,因为据我所知,Adobe Reader是台式机软件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM