Chrome window.print() window.close() results in &#

2020-02-05 07:28发布

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

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

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

13条回答
Animai°情兽
2楼-- · 2020-02-05 08:17

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

    setTimeout(function () { window.print(); }, 500);
    window.onfocus = function () { setTimeout(function () { window.close(); }, 500); }
查看更多
可以哭但决不认输i
3楼-- · 2020-02-05 08:18

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.

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!

查看更多
小情绪 Triste *
4楼-- · 2020-02-05 08:18

Now we have this event: 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();
查看更多
我命由我不由天
5楼-- · 2020-02-05 08:20

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

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.

查看更多
▲ chillily
6楼-- · 2020-02-05 08:21

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.

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.

查看更多
家丑人穷心不美
7楼-- · 2020-02-05 08:21

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();
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" />

查看更多
登录 后发表回答