Javascript window.print()后的确认消息
众所周知,无法确定用户在window.print()生成的打印窗口上单击了“打印”还是“取消” 因此,我坚持使用下一个最好的方法,就是基本上询问用户是否打印成功 我有这个:Javascript window.print()后的确认消息,javascript,jquery,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) {
$(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';
}
});
问题是,确认框在打印窗口之前弹出,如何确保打印窗口首先出现?有几种方法可以确保在运行代码块之前完成打印对话框。浏览器支持多种多样,因此您可能需要将其中一些结合起来以满足您的需要。请参阅下面的主题
window.onafterprint = function() {
var a = confirm('Confirm printing as successfull and mark Job Cards as Released?');
if(a == true) {
$.post('PrintSuccess');
window.location = '/Menu';
}
}
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,Sorry在chromecode中似乎不起作用,在chrome中工作,谢谢。if(window.onafterprint)位是如何工作的?它应该检查浏览器是否支持
onafterprint
事件,但它并不完全正确。如果('onafterprint'在窗口中)@ConnorBishop,window,它应该是if。onafterprint
是一个事件处理程序,在打印对话框完成后触发,但对于它来说非常有限。所以我们检查浏览器是否支持它,如果支持就使用它。如果浏览器不支持afterprint事件,我们将退回到setTimeout
解决方案(在chrome中工作)。我在代码中添加了注释来解释它。