window.print()之后的确认消息

时间:2016-08-08 10:57:01

标签: javascript jquery

众所周知,没有办法确定用户是否在window.print()生成的打印窗口上点击了打印或取消。

所以我坚持下一个最好的事情,就是基本上询问用户是否打印成功。

我有这个:

$(document).ready(function(){

    window.print();

    var a = confirm('Confirm printing as successfull and mark Job Cards as Released?');
    if(a == true) {
        $.post('PrintSuccess');
        window.location = '/Menu';
    }
});

麻烦的是,在打印窗口出现之前弹出确认框,如何确保首先出现打印窗口?

2 个答案:

答案 0 :(得分:2)

在运行代码块之前,有几种方法可以确保print dialog完成。浏览器支持各不相同,因此您可能需要将其中一些组合起来以满足您的需求。请参阅下面的stackoverflow主题。

以下代码适用于大多数现代浏览器。

var printCompleteCallback = function () {
    var conf = confirm('Confirm printing as successfull and mark Job Cards as Released?');
    if (conf) {
        if (a == true) {
            $.post('PrintSuccess');
            window.location = '/Menu';
        }
    }
}

window.print();
if (window.onafterprint) { //check if browser supports window.onafterprint event handler (Firefox and IE)
    $(window).one("afterprint", printCompleteCallback);
}
else { //Use setTimeout solution if onafterprint is not supported (Chrome)
    setTimeout(printCompleteCallback, 0);
}

答案 1 :(得分:0)

window.onafterprint = function() {
    var a = confirm('Confirm printing as successfull and mark Job Cards as Released?');
    if(a == true) {
        $.post('PrintSuccess');
        window.location = '/Menu';
    }
}