简体   繁体   English

在 X 秒后用 javascript 关闭打开的窗口(打印一个 div)

[英]Close opened window after X sec with javascript (print a div)

I want to print a div with javascript.我想用javascript打印一个div。 Here my code:这是我的代码:

<script type="text/javascript">
        function PrintDiv() {
            var divContents = document.getElementById("dvContents").innerHTML;
            var printWindow = window.open('', '', 'height=200,width=400');
            printWindow.document.write('<html><head><title>Nyomtatás</title>');
            printWindow.document.write('</head><body>');
            printWindow.document.write(divContents);
            printWindow.document.write('</body></html>');
            printWindow.document.close();
            self.close();
            printWindow.print();
        }
</script>

This code open the div content in a new window and start to print.此代码在新窗口中打开 div 内容并开始打印。 I want to close this windows with printWindow.document.close();我想用printWindow.document.close();关闭这个窗口 but it doesn't close.但它没有关闭。 Do you have idea how can I close it?你知道我该如何关闭它吗?

Use printWindow.close() instead of printWindow.document.close() and it will close the window.使用printWindow.close()而不是printWindow.document.close()它将关闭窗口。

https://jsfiddle.net/qba6h5up/1/ https://jsfiddle.net/qba6h5up/1/

This jsfiddle opens and closes a window after a set amount of milliseconds (you will see it if you have popups enabled).这个 jsfiddle 在设定的毫秒数后打开和关闭一个窗口(如果你启用了弹出窗口,你会看到它)。

Modify timeToCloseWindow variable to adjust the time of the window closing, or just add milliseconds directly.修改timeToCloseWindow变量来调整窗口关闭的时间,或者直接加毫秒。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM