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条回答
三岁会撩人
2楼-- · 2020-02-05 08:02

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

setTimeout(function () {
        mywindow.print();
        mywindow.close();
    }, 1000); 
查看更多
Ridiculous、
3楼-- · 2020-02-05 08:04

It work fine for me...

var newWin = window.open('', '');
//htmlPage is your html content 
setTimeout(newWin.document.write(htmlPage),1000);
// newWin.document.body.innerHTML = htmlPage;
newWin.document.close();
newWin.focus();
setTimeout(newWin.print(), 10000);
newWin.document.close();
newWin.close();
return false;
查看更多
smile是对你的礼貌
4楼-- · 2020-02-05 08:08

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 must return false or you get the error. This is true even if the link is a hash and goes nowhere!

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.

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

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

查看更多
祖国的老花朵
5楼-- · 2020-02-05 08:12

My solution to this:

window.print();
if(/Chrome/.test(navigator.userAgent)){
  $(window).focus(function(){
    window.location = '/print-thank-you';
  });
} else {
  window.location = '/print-thank-you';
}

EDIT: Printing on Chrome version >= 17 works just like IE/FF, so make sure that you account for that as well

查看更多
Deceive 欺骗
6楼-- · 2020-02-05 08:13

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

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

查看更多
我命由我不由天
7楼-- · 2020-02-05 08:14

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:

IE : 9.0.8...

FireFox : 15.01

Chrome : 23.0.1271.97 m

Safari : 5.1.7

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

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:

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.

查看更多
登录 后发表回答