[英]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在幕后做了类似的事情:
onclick
handler exists 请参阅onclick
处理程序 onclick
handler 调用onclick
处理程序 onclick
handler calls window.print()
(which blocks, waiting for user input) onclick
处理程序调用window.print()
(阻塞,等待用户输入) 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.