javascript 打印被 chrome 阻止

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5282719/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 16:38:09  来源:igfitidea点击:

Javascript print blocked by chrome

javascriptgoogle-chromeprinting

提问by Oppdal

I'm trying to run a javascript window.print()from chrome. It prints the first time but then subsequent calls (within a minute of the first call) fail. The chrome log states "Ignoring too frequent calls to print()."

我正在尝试window.print()从 chrome运行 javascript 。它第一次打印,但随后的调用(在第一次调用的一分钟内)失败。chrome 日志指出“忽略对 print() 的过于频繁的调用”。

     window.print();
 setInterval(function() {
     window.print();
 }, 5000);

Can't find a way around this issue. Any ideas?

找不到解决此问题的方法。有任何想法吗?

Thanks in advance

提前致谢

采纳答案by code_burgar

It appears to be a design decision rather than a bug. Getting around it will probably be pretty hard.

这似乎是一个设计决定而不是一个错误。绕过它可能会非常困难。

回答by Riley Lark

Sounds like a deliberate choice on Chrome's part that you probably won't be able to get around. As an alternative, you could prepare all of your jobs at once and separate them with a page break:

听起来像是 Chrome 的一个深思熟虑的选择,你可能无法绕过。作为替代方案,您可以一次准备所有作业并用分页符将它们分开:

<div style="page-break-after:always"></div>

<div style="page-break-after:always"></div>

回答by bvaughn

I have found the following to be a work-around to enable JavaScript printing from Chrome:

我发现以下是从 Chrome 启用 JavaScript 打印的解决方法:

<a href="#" onclick="window.print(); return false;">Click me to Print</a>

It seems that adding the "return false" bit to the onclick handler makes Chrome happy.

似乎将“返回假”位添加到 onclick 处理程序让 Chrome 高兴。

I think that without it, Chrome attempts to follow the link somewhere / reload the page. This results in a print dialog showing nothing to print.

我认为如果没有它,Chrome 会尝试在某处点击链接/重新加载页面。这会导致打印对话框显示无打印内容。

回答by corbacho

Good! Bug fixed. The bug was fixed as part of v.23 if I'm not wrong.

好的!错误已修复。如果我没记错的话,这个错误作为 v.23 的一部分被修复了。

So if the release cycle is every 6 weeks and Chrome 22 was released 25th of Sep, then by 6th of November(aprox.) the fix will be in the Chrome Stable version

因此,如果发布周期是每 6 周一次,并且 Chrome 22 是在 9 月 25 日发布的,那么到 11 月 6 日(大约),修复将在 Chrome 稳定版中

回答by John McLear

In chrome you have to fire the window.location.reload event to fire a print event.. ref from Chrome: window.print() print dialogue opens only after page reload (javascript)

在 chrome 中,您必须触发 window.location.reload 事件以触发打印事件.. 来自Chrome 的ref :window.print() 打印对话框仅在页面重新加载后打开 (javascript)

This works for me..

这对我有用..

window.print();
if(navigator.userAgent.toLowerCase().indexOf('chrome') > -1){
  window.location.reload();
}