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:23

my current hack. basically, just after window.print(), i bind mousemove, click, and keypress to trigger the action I want (window.close(); or window.location.href=somewhere;). Sure the action doesn't occur as long as the user does nothing. But it works pretty well. (well, i can live with until the bug is corrected at least)

/* 
// alternative to window.print() for dedicated print windows (had issue with <body onload=window.print()...
// not used anymore.
$('<a id="aprint" href="#" onclick="return window.print()" class="invisible"></a>').appendTo('body');
$('#aprint').trigger('click');
*/
$('body, select, input, textarea, button, submit, form').prop('disabled', true);
window.print();
setTimeout(function() {
    $('body').bind('mousemove click keypress', function () {
        window.location.href=home;                      
    });
}, 200);    
查看更多
登录 后发表回答