【问题标题】:window.print disables the parent windowwindow.print 禁用父窗口
【发布时间】:2025-11-26 07:10:02
【问题描述】:

我需要打印从后端系统发送的 html 转储。此打印是在从 UI 中单击按钮时触发的。我正在使用 window.print ,如下所示。这里的问题是,在加载打印预览时,它不应该阻止用户在父窗口中执行操作。但是,即使在新窗口上使用 window.print 时,父窗口也会被冻结和禁用。有没有办法在父窗口中启用用户访问?

以下代码用于打印:

function printElem(elem){
                var newwindow = window.open('', 'PRINT');
        newwindow.document.write('<html><head><title>' + 
                document.title + '</title>');
        newwindow.document.write('</head><body >');
        newwindow.document.write(elem);
        newwindow.document.write('</body></html>');
                newwindow.focus();
                newwindow.print();
                newwindow.close();
 }

【问题讨论】:

  • 你最好的办法是在back-end 上设置一个打印路由来处理打印哑数据,print() 方法会打开一个模态窗口,这意味着它将阻止用户返回到像警报窗口一样的原始窗口

标签: javascript


【解决方案1】:
$('#Button').attr('disabled','disabled');

在打印窗口打开之前添加此代码(var newwindow = window.open('', 'PRINT');)

【讨论】:

  • 你误解了这个问题,目标是“它不应该阻止用户执行操作”。
  • 您想在打开打印窗口的同时在父窗口上使用其他功能吗?