检测浏览器打印事件 [英] Detecting browser print event

查看:2458
本文介绍了检测浏览器打印事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能当用户从他们的浏览器打印的东西来检测?

Is it possible to detect when a user is printing something from their browser?

要复杂的是,如果我们是$ P $在新窗口中psenting使用PDF文档的用户,才有可能检测到文档的打印(从浏览器窗口假设用户打印出来)?

To complicate matters, if we are presenting a user with a PDF document in a new window is it possible to detect the printing of that document ( assuming the user prints it from the browser window)?

我已经能够找到最接近的是,如果我们实现自定义打印功能(类似于this)并跟踪被调用时

The closest I've been able to find is if we implement custom print functionality (something like this) and track when that is invoked

我在那的Internet Explorer(6或更高版本)

I'm primarily interested in a solution that works for internet explorer (6 or later)

推荐答案

您现在可以检测IE打印请求5+,火狐6+,铬9+和Safari 5+使用以下技术:

You can now detect a print request in IE 5+, Firefox 6+, Chrome 9+, and Safari 5+ using the following technique:

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

我进入更详细到这是什么做的,它可以在<一个用于什么href=\"http://tjvantoll.com/2012/06/15/detecting-print-requests-with-javascript/\">http://tjvantoll.com/2012/06/15/detecting-print-requests-with-javascript/.

这篇关于检测浏览器打印事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆