Javascript Chrome window.print() window.close() 导致“打印预览失败”。解决方案?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7652981/
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-08-24 03:04:38  来源:igfitidea点击:

Chrome window.print() window.close() results in 'print preview failed'. Solution?

javascriptgoogle-chrome

提问by Hoppe

I have a print page that opens in a new tab or window. The resulting page opens a print dialog. After the user makes a selection on the print dialog, the page then closes the tab/window.

我有一个在新选项卡或窗口中打开的打印页面。结果页面打开一个打印对话框。用户在打印对话框上做出选择后,页面将关闭选项卡/窗口。

window.print();
window.close();

This used to work great in the major browsers, but one of the latest versions of Chrome breaks this (i.e. 14.0.835.202).

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

I receive the following message from what I guess is the chrome print plugin: "Print preview failed".

我从我猜是 chrome 打印插件中收到以下消息:“打印预览失败”。

Does anyone have a solution to close the Chrome tab/window after printing?

有没有人有打印后关闭 Chrome 选项卡/窗口的解决方案?

采纳答案by Paul Grime

I don't think there are any standardised print events. IE has a couple, but I realise that doesn't help you for Chrome.

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

I think you might be left with only two options. A manual close button or some form of delay using setTimeout.

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

回答by darven

I just managed to come up with a solution that fits me. I started out with the reply from Peru but didn't want to use jQuery in this solution.

我只是设法想出了一个适合我的解决方案。我从秘鲁的回复开始,但不想在这个解决方案中使用 jQuery。

window.onload = function () {
  window.print();
  setTimeout(function(){window.close();}, 1);
}

For some reason Chrome dont start the timeout timer until the print dialog is closed.

出于某种原因,Chrome 在打印对话框关闭之前不会启动超时计时器。

回答by mrtsherman

This question is a top hit on Google so I thought I would add what I found, even though it does not exactly mirror your situation. If you have a link that calls window.print()then its onclick handler mustreturn false or you get the error. This is true even if the link is a hash and goes nowhere!

这个问题是谷歌上的热门话题,所以我想我会添加我的发现,即使它并不完全反映你的情况。如果您有一个调用的链接,window.print()那么它的 onclick 处理程序必须返回 false 否则您会收到错误消息。即使链接是散列并且无处可去,也是如此!

Print is unavailable because the page you were trying to print has been closed

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

To fix this make sure you add return false to the link.

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

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

Here is the Chromium bug that addresses this. It is marked as fixed for Chrome 17 (not yet released) and I have verified the fix in Chrome 18.

这是解决此问题的 Chromium 错误。它被标记为已针对 Chrome 17(尚未发布)修复,并且我已在 Chrome 18 中验证了该修复。

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

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

回答by SpYk3HH

Similar issue, I had.

类似的问题,我有。

FYI FOR ALL AS OF VER 17+ I was using the jquery plugin, printElement and had found a way to edit it so I could close the window, even in chrome ... then they updated to this new print thing! I have a solution yet again.

仅供参考,从 17+ 版开始,我正在使用 jquery 插件 printElement 并找到了一种编辑它的方法,这样我就可以关闭窗口,即使是在 chrome 中也是如此……然后他们更新了这个新的打印内容!我又有了解决办法。

Previous

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.

The NEW Problem

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).

The NEW Solution

I knew increasing the timer was no help, since it would just close at the end and could still stop the dialog from printing. Instead, I add a script link to the latest jquery build to the header of the new about page and then added a hover to the body of the page to be printed. On hover ... CLOSE!!! And guess what ... It works PERFECT!!!

In summery, if you're using a blank page to print in chrome and need to close the page after your element/whatever is printed, simply add a hover event on the body of that page (Or on inner html) that closes the window. Then, when you have selected a print method and hit print, it will print and close the dialog, leaving you that annoying window. Simply move your mouse a scooch and VIOLA! the annoying pop up window is GONE!

以前的

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.

新问题

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).

新的解决方案

我知道增加计时器没有帮助,因为它会在最后关闭并且仍然可以停止打印对话框。相反,我将最新 jquery 构建的脚本链接添加到新的关于页面的标题,然后在要打印的页面正文中添加了一个悬停。在悬停...关闭!你猜怎么着......它完美无缺!!!

总之,如果您使用空白页面在 chrome 中打印并且需要在元素/任何打印后关闭页面,只需在该页面的主体上(或在内部 html 上)添加一个悬停事件来关闭窗口. 然后,当您选择打印方法并点击打印时,它会打印并关闭对话框,留下烦人的窗口。只需将鼠标移动一个 scooch 和 VIOLA!烦人的弹出窗口不见了!

回答by Ahmad Alaa

All solution above doesn't solved my problem, using this code solved my problem

以上所有解决方案都没有解决我的问题,使用此代码解决了我的问题

setTimeout(function () {
        mywindow.print();
        mywindow.close();
    }, 1000); 

回答by Peru

add this script to your popup print window

将此脚本添加到您的弹出打印窗口

<script src="/assets/print_window_close.js" type="text/javascript"></script>

contents of print_window_close.js is

print_window_close.js 的内容是

jQuery(document).ready(function() {

    setTimeout(function() {
        window.close();
    }, 1);

});

basically seTimeout is called only when the document is focused which happens only when the overlay print dialog is closed. hence this works exactly the same way you wanted.

基本上 seTimeout 仅在文档​​被聚焦时调用,这仅在覆盖打印对话框关闭时发生。因此,这与您想要的方式完全相同。

回答by Clayton Cassar

For me i couldn't find a solution so i created one.

对我来说,我找不到解决方案,所以我创建了一个。

What i did is creating an invisible textbox and focus on it by using setInterval every 50ms after the window.print() is called. Also i bound the onfocus event to the textbox and when it get fired i close the window. This is tested on Chrome, Firefox and Internet Explorer.

我所做的是创建一个不可见的文本框,并在调用 window.print() 后每 50 毫秒使用一次 setInterval 来关注它。此外,我将 onfocus 事件绑定到文本框,当它被触发时,我关闭了窗口。这在 Chrome、Firefox 和 Internet Explorer 上进行了测试。

window.print();
setInterval(function () {
  document.getElementById("focustext").focus();
}, 50);
document.getElementById("focustext").onfocus = function () { window.close(); }
#focustext {
  border: none;
  height: 0px;
  width: 0px;
}
<input id="focustext" type="text" />

回答by Michael

Now we have this event: WindowEventHandlers.onafterprint (https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onafterprint).

现在我们有了这个事件:WindowEventHandlers.onafterprint ( https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onafterprint)。

With this event you can do the following:

通过此事件,您可以执行以下操作:

window.onafterprint = function() { 
    window.close();
};
window.print();

回答by Pakk

Tested today works for IE, FF, Chrome (including preview fix for chrome)

今天测试适用于 IE、FF、Chrome (包括 chrome 的预览修复)

    setTimeout(function () { window.print(); }, 500);
    window.onfocus = function () { setTimeout(function () { window.close(); }, 500); }

回答by Brett Weber

After an afternoon of research and thanks to @SpYk3HH's answer, I've found a solution that works in the versions I am running currently:

经过一个下午的研究并感谢@SpYk3HH 的回答,我找到了一个适用于我当前运行的版本的解决方案:

IE : 9.0.8...

浏览器:9.0.8 ...

FireFox : 15.01

火狐:15.01

Chrome : 23.0.1271.97 m

铬 : 23.0.1271.97 m

Safari : 5.1.7

Safari:5.1.7

When preparing the html to be displayed on a print specific page, include a check for

在准备要在打印特定页面上显示的 html 时,包括检查

if (oWindow == null || oWindow.closed || typeof(oWindow) == "undefined")

and then notify the user to enable popups temporarily. In order to allow your page to open, display the print dialog and close upon print dialog close add this function to the onload function for the window's body:

然后通知用户暂时启用弹出窗口。为了允许您的页面打开,显示打印对话框并在打印对话框关闭时关闭,将此函数添加到窗口主体的 onload 函数中:

function PrintPage()
{
    self.print();
    self.onmouseover = (function() { window.close(); }
}

The reason this works is because fundamentally we are still dealing with objects and events. The browsers may behave differently in some things, but they all have common ground to look for. A simple onmouseover event will hopefully always happen when a window gains focus, and so this code will hopefully not lose its relevance.

这样做的原因是因为从根本上讲,我们仍在处理对象和事件。浏览器在某些方面的行为可能有所不同,但它们都有共同点可以寻找。当窗口获得焦点时,一个简单的 onmouseover 事件有望总是发生,因此这段代码有望不会失去其相关性。