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 div in my page which store information about the voucher.

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.

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?

推荐答案

您可以收听afterprint事件.

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.

(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;

}());

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

Jquery相关问答推荐

jQuery .append() 创建两个元素而不是一个

更改高亮 colored颜色

通过 .push() 方法向对象添加项目

如何使用 aria-expanded="true" 更改 CSS 属性

将变量传递给jQuery AJAX成功回调中的函数

如何访问 JSON 对象名称/值?

在 contenteditable div 中的插入符号处插入 html

javascript:检测滚动结束

jQuery 对象和 DOM 元素

子元素点击事件触发父点击事件

使用 JavaScript 和 jQuery,跨浏览器处理按键事件 (F1-F12)

jQuery - 使用发布数据重定向

jQuery.extend 和 jQuery.fn.extend 的区别?

datatable jquery - 表头宽度与正文宽度不对齐

如何从 jQuery UI datepicker 获取日期

延迟jquery悬停事件?

获取没有在css中设置高度的div的高度

!!~(不是波浪号/bang bang 波浪号)如何改变包含/包含数组方法调用的结果?

使用 jQuery 获取选中复选框的值

jquery追加到列表的前面/顶部