oboshto oboshto - 3 years ago 165
Javascript Question

window.close(); after window.print(); javascript

I need a js code which will close window right after appearing of pop up print window. Here is my code:

<a href="javascript:;" onclick="print()">Print</a>

function print()
{
win = window.open();
win.document.write('<html><head>blablabla.......');
win.window.print();
win.window.close();
}


But it doesn't work because window closes before it pop up.

Answer Source
<a href="javascript:;" onclick="print()">Print</a>

    function print()
    {   
        win = window.open();
        win.document.write('<html><head>blablabla.......');

        var document_focus = false; // var we use to monitor document focused status.
        // Now our event handlers.
        $(document).ready(function() { win.window.print();document_focus = true; });
        setInterval(function() { if (document_focus === true) { win.window.close(); }  }, 300);
    }

Thanks Stilltorik for link.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download