web端js调用window.print()的回调函数(打印前和打印后的回调)

2,301 阅读1分钟
$(function () {
    //定义打印前事件 
    var beforePrint = function () {
        console.log("打印前");
    };
    //定义打印后事件 
    var afterPrint = function () {
        console.log("打印后");
        app.post('ba.service.CompanyService', 'updateCompany', {
            'companyId': companyId,
            "autoRegStateCode":"40"
        })
            .then(function (res) {
            });
    }

    //监听window状态
    if (window.matchMedia) {
        var mediaQueryList = window.matchMedia('print');

        //为印添加事件
        mediaQueryList.addListener(function (mql) {
            if (mql.matches) {
                beforePrint();
            } else {
                afterPrint();
            }
        });
    }

    //打印前事件
    window.onbeforeprint = beforePrint;

    //打印后事件
    window.onafterprint = afterPrint;
    //执行打印
    $(".print").click(function () {
        $(".print").hide()
        var newstr = document.getElementById("printPage").innerHTML;
        window.print();
        $(".print").show()
    })
});

Ps:谷歌上调用,回调前,回调后,两个函数,会各自调用两次