JavaScript 打印行为监听

var beforePrint = function () {
    //alert('Functionality to run before printing.');
    //开始打印
};
var afterPrint = function () {
    //alert('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)