Home Blog JavaScript Close browser window after print
Close browser window after print
JavaScript
Written by Dicky   
Tuesday, 03 April 2012 15:31
AddThis Social Bookmark Button

If you need a simple JavaScript function to display the print dialog when the page is loaded and close by itself when it's done printing.

Use the following code:

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

    function CheckWindowState()    {           
        if(document.readyState=="complete") {
            window.close(); 
        } else {           
            setTimeout("CheckWindowState()", 2000)
        }
    }
    PrintWindow();
</script> 

Simply stick it in before your </head> tag and voila!