【问题标题】:Is it possible to close the print dialog?是否可以关闭打印对话框?
【发布时间】:2016-11-30 21:32:34
【问题描述】:

通过使用window.print(),我们可以触发打开打印对话框。

有什么办法可以关闭吗?

事实证明(至少在 Chrome/Chromium 上),location.reload() 确实关闭了它,但它会重新加载页面。

还有其他更友好的方式/API 来关闭它吗?

【问题讨论】:

    标签: javascript browser printing


    【解决方案1】:

    您可以查看window.close。这将关闭当前窗口。

    var openPrint = window.print();
    //Rest of code
     openPrint.close();
    

    【讨论】:

    • window.print() 返回undefined。我想关闭打印对话框,而不是窗口。
    【解决方案2】:

    试试这个:

    var window = window.open();
    window.print();
    window.onfocus = function () {
        window.close();
    }
    

    【讨论】:

      【解决方案3】:

      使用setTimeout怎么样?

      window.print(); //this triggers the print
      
      setTimeout("closePrintView()", 3000); // delay required for IE to realise what's going on
      
      window.onafterprint = closePrintView(); // this is the magic
      
      // this function simply runs something you want it to do 
      function closePrintView() {
        // do something
      }
      

      编辑:如果有用的话,还有onbeforeprint。

      编辑 2:似乎onbeforeprint 触发之前对话框出现...

      onafterprint 被触发就在对话框出现之前。因此无法知道文档是否已打印或您是否已取消对话框。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-21
        • 1970-01-01
        相关资源
        最近更新 更多