Javascript 打印对话框关闭后自动关闭窗口

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

Close window automatically after printing dialog closes

javascriptprinting

提问by Tsundoku

I have a tab open when the user clicks a button. On the onloadI have it bring up the print dialog, but the user asked me whether it was possible that after it sends to the printer to print, if the tab could close itself. I am not sure whether this can be done. I have tried using setTimeout();, but it's not a defined period of time since the user might get distracted and have to reopen the tab. Is there any way to accomplish this?

当用户单击按钮时,我打开了一个选项卡。在onload我把它调出打印对话框,但用户问我它是否是可能的,它发送到打印机后打印,如果标签可以关闭本身。我不确定这是否可以做到。我曾尝试使用setTimeout();,但它不是一个定义的时间段,因为用户可能会分心并不得不重新打开选项卡。有什么办法可以做到这一点吗?

回答by serfer2

if you try to close the window just after the print() call, it may close the window immediately and print() will don't work. This is what you should not do:

如果您尝试在 print() 调用后立即关闭窗口,它可能会立即关闭窗口并且 print() 将不起作用。这是你不应该做的

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

This solution will work in Firefox, because on print() call, it waits until printing is done and then it continues processing javascript and close() the window. IE will fail with this because it calls the close() function without waiting for the print() call is done. The popup window will be closed before printing is done.

此解决方案将在 Firefox 中工作,因为在调用 print() 时,它会等待打印完成,然后继续处理 javascript 并关闭窗口。IE 将因此失败,因为它调用 close() 函数而不等待 print() 调用完成。弹出窗口将在打印完成前关闭。

One way to solve it is by using the "onafterprint" event but I don' recommend it to you becasue these events only works in IE.

解决它的一种方法是使用“onafterprint”事件,但我不向您推荐它,因为这些事件仅适用于 IE。

The best wayis closing the popup window once the print dialog is closed (printing is done or cancelled). At this moment, the popup window will be focussed and you can use the "onfocus" event for closing the popup.

最好的方法是在打印对话框关闭(打印完成或取消)后关闭弹出窗口。此时,弹出窗口将被聚焦,您可以使用“onfocus”事件关闭弹出窗口。

To do this, just insert this javascript embedded code in your popup window:

为此,只需在弹出窗口中插入此 javascript 嵌入代码:

<script type="text/javascript">
window.print();
window.onfocus=function(){ window.close();}
</script>

Hope this hepls ;-)

希望这有助于 ;-)

Update:

更新:

For new chrome browsers it may still close too soon see here. I've implemented this change and it works for all current browsers: 2/29/16

对于新的 chrome 浏览器,它可能仍然过早关闭,请参见此处。我已经实现了这个更改,它适用于所有当前的浏览器:2/29/16

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

回答by noamtcohen

This is what I came up with, I don't know why there is a small delay before closing.

这是我想出来的,不知道为什么关闭前会有一点延迟。

 window.print();
 setTimeout(window.close, 0);

回答by Ying Style

Just:

只是:

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

It works.

有用。

回答by Holger

I just want to write what I have done and what has worked for me (as nothing else I tried had worked).

我只想写下我所做的和对我有用的东西(因为我尝试过的其他方法都没有奏效)。

I had the problem that IE would close the windows before the print dialog got up.

我的问题是 IE 会在打印对话框出现之前关闭窗口。

After a lot of trial and error og testing this is what I got to work:

经过大量的反复试验 og 测试后,这就是我的工作:

var w = window.open();
w.document.write($('#data').html()); //only part of the page to print, using jquery
w.document.close(); //this seems to be the thing doing the trick
w.focus();
w.print();
w.close();

This seems to work in all browsers.

这似乎适用于所有浏览器。

回答by Victor Olaru

This code worked perfectly for me:

这段代码非常适合我:

<body onload="window.print()" onfocus="window.close()">

When the page opens it opens the print dialog automatically and after print or cancel it closes the window.

当页面打开时,它会自动打开打印对话框,并在打印或取消后关闭窗口。

Hope it helps,

希望能帮助到你,

回答by Heroselohim

This is a cross-browser solution already testedon Chrome, Firefox, Opera by 2016/05.

这是一个跨浏览器的解决方案,已于2016/05 年在 Chrome、Firefox、Opera 上进行了测试。

Take in mind that Microsoft Edge has a bugthat won't close the window if print was cancelled. Related Link

请记住,Microsoft Edge 有一个错误,如果取消打印,该错误将不会关闭窗口。相关链接

var url = 'http://...';
var printWindow = window.open(url, '_blank');
printWindow.onload = function() {
    var isIE = /(MSIE|Trident\/|Edge\/)/i.test(navigator.userAgent);
    if (isIE) {

        printWindow.print();
        setTimeout(function () { printWindow.close(); }, 100);

    } else {

        setTimeout(function () {
            printWindow.print();
            var ival = setInterval(function() {
                printWindow.close();
                clearInterval(ival);
            }, 200);
        }, 500);
    }
}

回答by Nado11

Using Chrome I tried for a while to get the window.onfocus=function() { window.close(); }and the <body ... onfocus="window.close()">to work. My results:

使用 Chrome 我尝试了一段时间来让window.onfocus=function() { window.close(); }<body ... onfocus="window.close()">工作。我的结果:

  1. I had closed my print dialogue, nothing happened.
  2. I changed window/tabs in my browser, still nothing.
  3. changed back to my first window/tab and then the window.onfocus event fired closing the window.
  1. 我关闭了打印对话框,什么也没发生。
  2. 我在浏览器中更改了窗口/选项卡,仍然没有。
  3. 改回我的第一个窗口/选项卡,然后 window.onfocus 事件触发关闭窗口。

I also tried <body onload="window.print(); window.close()" >which resulted in the window closing before I could even click anything in the print dialogue.

我还尝试过<body onload="window.print(); window.close()" >导致窗口关闭,然后我什至可以单击打印对话框中的任何内容。

I couldn't use either of those. So I used a little Jquery to monitor the document status and this code works for me.

我不能使用其中任何一个。所以我使用了一个小 Jquery 来监控文档状态,这段代码对我有用。

<script type="text/javascript">
    var document_focus = false; // var we use to monitor document focused status.
    // Now our event handlers.
    $(document).focus(function() { document_focus = true; });
    $(document).ready(function() { window.print(); });
    setInterval(function() { if (document_focus === true) { window.close(); }  }, 500);
</script>

Just make sure you have included jquery and then copy / paste this into the html you are printing. If the user has printed, saved as PDF or cancelled the print job the window/tab will auto self destruct. Note: I have only tested this in chrome.

只需确保您已包含 jquery,然后将其复制/粘贴到您正在打印的 html 中。如果用户已打印、保存为 PDF 或取消打印作业,窗口/选项卡将自动自毁。注意:我只在 chrome 中测试过这个。

Edit

编辑

As Jypsy pointed out in the comments, document focus status is not needed. You can simply use the answer from noamtcohen, I changed my code to that and it works.

正如 Jypsy 在评论中指出的那样,不需要文档焦点状态。您可以简单地使用 noamtcohen 的答案,我将代码更改为该答案并且可以正常工作。

回答by Samuel Bié

Sure this is easily resolved by doing this:

当然,通过这样做很容易解决这个问题:

  <script type="text/javascript">
     window.print();
     window.onafterprint = window.close();
  </script>

回答by Md Abu Sohib Hossain

The following worked for me:

以下对我有用:

function print_link(link) {
    var mywindow = window.open(link, 'title', 'height=500,width=500');   
    mywindow.onload = function() { mywindow.print(); mywindow.close(); }
}

回答by arun.m

this one works for me:

这个对我有用:

<script>window.onload= function () { window.print();window.close();   }  </script>