简体   繁体   English

如何从javascript显示打印对话框而不会收到“Page Unresponsive”警告?

[英]How to show print dialog from javascript without getting “Page Unresponsive” warnings?

I've got a page with a simple "print" image/link that executes a simple javascript onclick handler. 我有一个页面,带有一个简单的“打印”图像/链接,执行一个简单的javascript onclick处理程序。 I'm finding that Chrome will complain about the page with a "Page Unresponsive" warning when I take too long to inspect the print dialog. 我发现当我花费太长时间检查打印对话框时,Chrome会抱怨页面上出现“页面无响应”警告。 I do not see this behavior in other browsers. 我没有在其他浏览器中看到此行为。

The HTML looks like this: HTML看起来像这样:

<a onclick="if(window.print) window.print(); return false;">
  <img src="print-button.png" alt="[Print]" />
</a>

There are no other active scripts on the page, so I'm guessing that this onclick handler is the problem. 页面上没有其他活动脚本,所以我猜这个onclick处理程序是问题所在。 My guess is that Chrome does something like this behind the scenes: 我的猜测是Chrome在幕后做了类似的事情:

  1. Accept click event from OS 从OS接受点击事件
  2. See onclick handler exists 请参阅onclick处理程序
  3. Start stopwatch 启动秒表
  4. Invoke onclick handler 调用onclick处理程序
  5. onclick handler calls window.print() (which blocks, waiting for user input) onclick处理程序调用window.print() (阻塞,等待用户输入)
  6. [time passes] [时间流逝]
  7. Stopwatch indicates that too much time has passed 秒表表示已经过了太多时间
  8. Chrome issues a warning to the user Chrome会向用户发出警告

I would consider this a bug in Chrome, if true, since the window.print call shouldn't be "timed". 我认为这是Chrome中的一个错误,如果是的话,因为window.print调用不应该“定时”。 That is, it's a blocking user-interactive action and the user is allowed to take as much time as they need. 也就是说,它是一个阻止用户交互的动作,并允许用户花费他们需要的时间。

I've tried cheating by doing this: 我这样做曾经试过作弊:

<a onclick="if(window.print) setTimeout(function() { window.print(); }, 0); return false;">
  <img src="print-button.png" alt="[Print]" />
</a>

... but it does not change anything -- Chrome still complains about the long-running script. ...但它没有改变任何东西 - Chrome仍然抱怨长期运行的脚本。

Am I using the wrong technique to trigger the print dialog? 我使用错误的技术来触发打印对话框吗? Or is this a bug in Chrome? 或者这是Chrome中的错误?

I am running v72.0.3626.119 on 64-bit Mac and I cannot reproduce this while I have a client running the same version in 64-bit Windows and she is reporting this issue. 我在64位Mac上运行v72.0.3626.119并且我无法重现这一点,而我在64位Windows中运行相同版本的客户端并且她正在报告此问题。

This appears to be a known bug in Chrome since Feb 27 2018 / Chrome 64 自2018年2月27日/ Chrome 64以来,这似乎是Chrome中的已知错误

There is a fix slated for Chrome 73 Chrome 73有一个修复程序

https://bugs.chromium.org/p/chromium/issues/detail?id=816869 https://bugs.chromium.org/p/chromium/issues/detail?id=816869

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从网页打印而不显示打印对话框 - How to print from web page without showing print dialog 打印网页而不显示打印对话框和此网页 - print web page without show the print dialog and this web page 如何在不打开打印对话框的情况下使用 google chrome 中的 javascript 进行打印? - How to print without opening the print dialog using javascript in google chrome? 我如何使用javascript或php从Web浏览器中打印页面或页面的一部分而无需打印窗口 - how i can print a page or part of the page from web browser without print window using javascript or php Javascript-如何在首次显示后禁用打印对话框? - Javascript - How to disable the print dialog after first show? 如何在没有打印对话框的情况下从Web浏览器进行打印? - How to print from web browser without print dialog? javascript打印没有打印对话框 - javascript print without print dialog box 如何在没有任何对话框的情况下发送数据进行打印并在javascript中进行预览 - how send data to print without any dialog box and preview in javascript 如何在laravel中使用javascript在没有预览对话框的情况下打印 - How to print without preview dialog box using javascript in laravel 使用php(javascript)在没有Windows打印对话框的情况下在本地打印页面 - Print a page on local using php (javascript) without Windows print dialog box
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM