【问题标题】:Problem: Jquery printing close window immediately after open问题:打开后立即关闭窗口的Jquery打印
【发布时间】:2019-11-03 09:13:29
【问题描述】:

在某些 Chrome 浏览器中当用户点击打印按钮时立即关闭。

我的代码是:

newWin = window.open("");
newWin.document.write("some content");
newWin.print();
newWin.close();

我试过了:

newWin.focus();
newWin.print();
newWin.close();

还有:

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

还有:

var document_focus = false; // var we use to monitor document focused status.

// Now our event handlers.
$(document).ready(function() { newWin.window.print();document_focus = true; });
setInterval(function() { if (document_focus === true) { newWin.window.close(); }  }, 300);

但它们都不起作用,打印窗口仍然关闭。

如何防止打印窗口关闭?

谢谢。

【问题讨论】:

    标签: jquery google-chrome printing


    【解决方案1】:

    我已经使用 iFrame 解决了这个问题

    iFrame:

     <iframe id="print_this_iframe" hidden></iframe>
    

    Jquery:

    var dstFrame = document.getElementById('print_this_iframe');
    var dstDoc = dstFrame.contentDocument || dstFrame.contentWindow.document;
    dstDoc.write('Some Content');
    dstDoc.close();
    dstFrame.onload = function() { $("#print_this_iframe").get(0).contentWindow.print()}; 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-20
      • 1970-01-01
      • 2019-06-21
      相关资源
      最近更新 更多