jQuery 如何检测 window.print() 完成

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18325025/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 21:22:35  来源:igfitidea点击:

How to detect window.print() finish

javascriptjquery

提问by JavaScripter

In my application, I tried to print out a voucher page for the user like this:

在我的应用程序中,我尝试为用户打印一个凭证页面,如下所示:

  var htm ="<div>Voucher Details</div>";
  $('#divprint').html(htm);
  window.setTimeout('window.print()',2000);

'divprint' is a divin my page which store information about the voucher.

' divprint' 是div我的页面中的一个,用于存储有关凭证的信息。

It works, and the print page pops up. But I want to advance the application once the user clicks 'print' or 'close' in the browser's pop-up print dialog.

它有效,并弹出打印页面。但是我想在用户在浏览器的弹出打印对话框中单击“ print”或“ close”后推进应用程序。

For example, I'd like to redirect user to another page after pop up window is closed:

例如,我想在弹出窗口关闭后将用户重定向到另一个页面:

window.application.directtoantherpage();//a function which direct user to other page

How can I determine when the pop up print window is closed or print is finished?

如何确定弹出打印窗口何时关闭或打印完成?

回答by Adam

You can listen to the afterprint event.

您可以收听 afterprint 事件。

https://developer.mozilla.org/en-US/docs/Web/API/window.onafterprint

https://developer.mozilla.org/en-US/docs/Web/API/window.onafterprint

window.onafterprint = function(){
   console.log("Printing completed...");
}

It may be possible to use window.matchMedia to get this functionality in another way.

可以使用 window.matchMedia 以另一种方式获得此功能。

(function() {

    var beforePrint = function() {
        console.log('Functionality to run before printing.');
    };

    var afterPrint = function() {
        console.log('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;

}());

Source: http://tjvantoll.com/2012/06/15/detecting-print-requests-with-javascript/

来源:http: //tjvantoll.com/2012/06/15/detecting-print-requests-with-javascript/

回答by Sagreen

On chrome (V.35.0.1916.153 m) Try this:

在 chrome (V.35.0.1916.153 m) 上试试这个:

function loadPrint() {
    window.print();
    setTimeout(function () { window.close(); }, 100);
}

Works great for me. It will close window after user finished working on printing dialog.

对我很有用。用户完成打印对话框后,它将关闭窗口。

回答by SAM

compatible with chrome, firefox, opera, Internet Explorer
Note: jQuery required.

兼容 chrome、firefox、opera、Internet Explorer
注意:需要 jQuery。

<script>

    window.onafterprint = function(e){
        $(window).off('mousemove', window.onafterprint);
        console.log('Print Dialog Closed..');
    };

    window.print();

    setTimeout(function(){
        $(window).one('mousemove', window.onafterprint);
    }, 1);

</script>

回答by quietmint

See https://stackoverflow.com/a/15662720/687315. As a workaround, you can listen for the afterPrintevent on the window (Firefox and IE) and listen for mouse movement on the document (indicating that the user has closed the print dialog and returned to the page) after the window.mediaMatchAPI indicates that the media no longer matches "print" (Firefox and Chrome).

请参阅https://stackoverflow.com/a/15662720/687315。作为一种解决方法,您可以afterPrintwindow.mediaMatchAPI 指示媒体没有更长的匹配“打印”(Firefox 和 Chrome)。

Keep in mind that the user may or may not have actually printed the document. Also, if you call window.print()too often in Chrome, the user may not have even been prompted to print.

请记住,用户可能已经或可能没有实际打印过文档。此外,如果您window.print()在 Chrome 中调用过于频繁,甚至可能不会提示用户进行打印。

回答by user3017502

You can detect when window.print() is finished simply by putting it in another function

您可以通过将 window.print() 放在另一个函数中来检测它何时完成

//function to call if you want to print
var onPrintFinished=function(printed){console.log("do something...");}

//print command
onPrintFinished(window.print());

tested in Firefox,Google chrome,IE

在 Firefox、谷歌浏览器、IE 中测试

回答by Rahil Ahmad

window.print behaves synchronously on chrome .. try this in your console

window.print 在 chrome 上同步运行 .. 在你的控制台中试试这个

window.print();
console.log("printed");

"printed" doesn't display unless the print dialog is closed(canceled/saved/printed) by the user.

除非用户关闭(取消/保存/打印)打印对话框,否则“打印”不会显示。

Here is a more detailed explanation about this issue.

这里有一个关于这个问题的更详细的解释。

I am not sure about IE or Firefox will check and update that later

我不确定 IE 或 Firefox 会稍后检查并更新

回答by FriendlyDev

Given that you wish to wait for the print dialog to go away I would use focus binding on the window.

鉴于您希望等待打印对话框消失,我将在窗口上使用焦点绑定。

print();

var handler = function(){
    //unbind task();
    $(window).unbind("focus",handler);
}

$(window).bind("focus",handler);

By putting in the unbind in the handler function we prevent the focus event staying bond to the window.

通过在处理函数中加入 unbind,我们可以防止焦点事件与窗口保持绑定。

回答by Hyman Franzen

This Actually worked for me in chrome. I was pretty suprised.

这实际上在 chrome 中对我有用。我很惊讶。

jQuery(document).bind("keyup keydown", function(e){
    if(e.ctrlKey && e.keyCode == 80){
         Print(); e.preventDefault();
    }
});

Where Print is a function I wrote that calls window.print(); It also works as a pure blocker if you disable Print();

其中 Print 是我编写的一个函数,它调用 window.print(); 如果您禁用 Print(),它也可用作纯拦截器;

As noted here by user3017502

正如 user3017502 在这里指出的

window.print() will pause so you can add an onPrintFinish or onPrintBegin like this

window.print() 将暂停,因此您可以像这样添加 onPrintFinish 或 onPrintBegin

function Print(){
    onPrintBegin
    window.print();
    onPrintFinish(); 
}

回答by Pakk

Tested IE, FF, Chrome and works in all.

测试了 IE、FF、Chrome 并在所有方面都有效。

    setTimeout(function () { window.print(); }, 500);
    window.onfocus = function () { setTimeout(function () { window.close(); }, 500); }

回答by e-info128

Print in new window with w = window.open(url, '_blank') and try w.focus();w.close(); and detect when page is closed. Works in all browsers.

使用 w = window.open(url, '_blank') 在新窗口中打印并尝试 w.focus();w.close(); 并检测页面何时关闭。适用于所有浏览器。

w = window.open(url, '_blank');
w.onunload = function(){
 console.log('closed!');
}
w.focus();
w.print();
w.close();

Window close after finish print.

打印完成后关闭窗口。