【问题标题】:window.print() from Chrome subwindow - close, then breaksChrome 子窗口中的 window.print() - 关闭,然后中断
【发布时间】:2014-05-20 05:09:01
【问题描述】:

当在我们的 web 应用程序中启动 Chrome 子窗口时,通过 window.open() 或用户单击带有 target="_blank" 的链接,然后在该子窗口中,正文 onload="window.print()" 自动启动打印对话框和打印预览,然后用户关闭打印/子窗口而不是单击取消,父窗口完全被冲洗掉。具体来说:

  1. 不会触发任何 javascript 事件
  2. 没有可点击的链接
  3. 按 F5 会在选项卡中显示小微调器,但页面永远不会重新加载。
  4. 父窗口真的死了——你能做的就是关闭它。

如果您在子窗口(通过window.print() 启动打印预览)上单击取消,一切都很好。但是如果用户关闭窗口,所有的疯狂都会发生。

这是 Chrome 中的一个已知错误:

有人知道解决方法吗?

【问题讨论】:

标签: javascript google-chrome


【解决方案1】:

不要使用window.print(),只需将您的内容带到新窗口,然后调用下面给出的打印功能来打印内容。

以下是一个函数调用,我们将元素的内部 html 通过它的 id 传递到新窗口。

PrintContent(document.getElementById('div-content-id').innerHTML);

function PrintContent(printableContent) {
    var printWindow = window.open("", "Print_Content", 'scrollbars=1,width=900,height=900top=' + (screen.height - 700) / 2 + ',left=' + (screen.width - 700) / 2);
    printWindow.document.write(printableContent);
    printWindow.document.close();
    printWindow.focus();
    printWindow.print();
    printWindow.close();
    return false;
}

我也遇到了同样的问题,它对我有用。

【讨论】:

    猜你喜欢
    • 2023-01-12
    • 2020-01-25
    • 2015-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-14
    • 2012-01-26
    • 1970-01-01
    相关资源
    最近更新 更多