Chrome window.print()window.close()导致'打印预览失败'.解?

Hop*_*ppe 13 javascript google-chrome

我有一个打印页面,在新标签页或窗口中打开.结果页面打开一个打印对话框.用户在打印对话框上进行选择后,页面将关闭选项卡/窗口.

window.print();
window.close();
Run Code Online (Sandbox Code Playgroud)

这曾经在主流浏览器中运行良好,但Chrome的最新版本之一打破了这一点(即14.0.835.202).

我收到以下消息,我猜是chrome打印插件:"打印预览失败".

有没有人有解决方案在打印后关闭Chrome标签/窗口?

小智 31

我刚刚设法找到适合我的解决方案.我从秘鲁的回复开始,但不想在这个解决方案中使用jQuery.

window.onload = function () {
  window.print();
  setTimeout(function(){window.close();}, 1);
}
Run Code Online (Sandbox Code Playgroud)

出于某种原因,Chrome不会启动超时计时器,直到打印对话框关闭.


mrt*_*man 11

这个问题在Google上是最受欢迎的,所以我想我会添加我发现的内容,即使它并不能完全反映您的情况.如果你有一个调用的链接,window.print()那么它的onclick处理程序必须返回false或你得到错误.即使链接是哈希并且无处可去,也是如此!

打印不可用,因为您尝试打印的页面已关闭

要解决此问题,请确保在链接中添加return false.

<a href="#" onclick="window.print(); return false;" >Print</a>
Run Code Online (Sandbox Code Playgroud)

这是解决这个问题的Chromium bug.Chrome 17标记为已修复(尚未发布),我已在Chrome 18中验证了此修复程序.

http://code.google.com/p/chromium/issues/detail?id=92107


Pau*_*ime 6

我认为没有任何标准化的打印事件.IE有两个,但我意识到这对Chrome没有帮助.

我想你可能只有两种选择.手动关闭按钮或某种形式的延迟使用setTimeout.

  • 我以前有过:window.print(); setTimeout('window.close()',1000); 这曾经与Chrome一起使用,但它在最新版本中停止了工作.Chrome报告:"打印不可用,因为您尝试打印的页面已关闭." 如果有人质疑关闭按钮是唯一的选择,请告诉我!谢谢! (2认同)

SpY*_*3HH 5

类似的问题,我有.

仅供参考我们使用jquery插件,printElement并找到了编辑它的方法,因此我可以关闭窗口,即使是在Chrome中......然后他们更新到这个新的印刷品!我又有了一个解决方案.

以前

Chrome used to open a seperate window containing just the element i wanted 
printed and its relative css.  That window then spawned the .print command 
which opened another window containing print preview. By adding a simple 50 
millisecond timeout i was able to close the previous window without hurting 
the print preview.
Run Code Online (Sandbox Code Playgroud)

新问题

Chrome no longer opens a new window for their print preview. Instead it 
opens a "dialog with overlay" in the current page to be printed (aka, our 
new blank page containing just the element for print). Thus, when my timer 
kicked in, it wouldn't close the page immediately, but as soon as the dialog 
code closed.  The problem was, it closed so fast, the print command never 
made it to its service and thus nothing was printed to pdf 
(what our clinic uses).
Run Code Online (Sandbox Code Playgroud)

新的解决方案

我知道增加计时器没有任何帮助,因为它只会在结束时关闭,仍然可以停止打印对话框.相反,我将最新的jquery构建的脚本链接添加到新的about页面的标题,然后将鼠标悬停添加到要打印的页面主体.在悬停...关闭!并猜猜是什么......它的工作完美!

总而言之,如果您使用空白页面以chrome打印并需要在元素/打印之后关闭页面,只需在该页面的主体(或内部html)上添加一个悬停事件即可关闭窗口.然后,当您选择了打印方法并点击打印时,它将打印并关闭对话框,留下那个烦人的窗口.只需将您的鼠标移动一个scooch和VIOLA!恼人的弹出窗口已经消失了!