【问题标题】:Confirmation message after window.print()window.print() 后的确认消息
【发布时间】:2016-08-08 10:57:01
【问题描述】:

众所周知,在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';
    }
});

问题是,确认框先于打印窗口弹出,如何保证打印窗口先出现?

【问题讨论】:

  • 在 chrome 中似乎不起作用

标签: javascript jquery


【解决方案1】:

有几种方法可以确保在运行代码块之前完成 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);
}

【讨论】:

  • 代码在 chrome 中工作,谢谢。 if(window.onafterprint) 位是如何工作的?
  • 它应该检查浏览器是否支持onafterprint事件,但它并不完全正确。应该是if( 'onafterprint' in window)
  • @ConnorBishop,window.onafterprint 是一个在打印对话框完成后触发的事件处理程序,但 browser support 非常有限。所以我们检查浏览器是否支持它,如果支持就使用它。如果浏览器不支持 afterprint 事件,我们会退回到setTimeout 解决方案(在 chrome 中工作)。我在解释它的代码中添加了 cmets。
【解决方案2】:
window.onafterprint = function() {
    var a = confirm('Confirm printing as successfull and mark Job Cards as Released?');
    if(a == true) {
        $.post('PrintSuccess');
        window.location = '/Menu';
    }
}

【讨论】:

  • 应该提到我正在使用 chrome,抱歉
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-01-13
  • 1970-01-01
  • 2023-03-08
  • 2019-02-04
  • 2021-10-28
  • 2012-05-10
  • 1970-01-01
相关资源
最近更新 更多