2014-01-28 506 views
1

我需要一個js代碼,在出現彈出窗口後立即關閉窗口。這是我的代碼:window.close();在window.print();之後javascript

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

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

但它不起作用,因爲窗口在它彈出之前關閉。

+1

刪除'win.window.close();' –

+0

@DontVoteMeDown關閉window..how地球上這將工作..你應該保持活動窗口,直到你打印...並且不要辱罵和冒犯..凱? –

+2

我想你可以找到你要找的[這裏](http://stackoverflow.com/questions/6460630/close-window-automatically-after-printing-dialog-closes) – Stilltorik

回答

1
<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); 
    } 

感謝Stilltorik對於link

2

這樣,打印後窗口會關閉,工作完美!

function printme() {   
    var newWindow = window.open(); 
    newWindow.document.open("text/html"); 
    newWindow.document.write('<html><head>blablabla.......'); 
    newWindow.document.close(); 

    newWindow.print(); 
    newWindow.onfocus = function() { 
     newWindow.close(); // Close the window 
    } 
}; 
2

此代碼爲我工作:

print = function() { 
    var printContents = document.getElementById('div').innerHTML, 
     params = [ 
     'height=' + screen.height, 
     'width=' + screen.width, 
     'fullscreen=yes'].join(','), 

    popup = window.open("", 'popUpWindow', params); 
    popup.document.write('<html><head><link rel="stylesheet" type="text/css" href="/style.css" /></head><body class="print" onload="window.print()" onmousemove="setTimeout(function() {window.close()},1000);" >' 
     + printContents 
     + '</body></html>'); 
    popup.onfocus = function() { 
     setTimeout(function() { 
     popup.focus(); 
     popup.document.close(); 
     }, 200); 
    }; 
}; 
0

這段代碼在JQuery打印也是HTML渲染裏面的動態圖像。

<button id="printreceipt"> 
    Print 
    </button> 
    <div id="receipt"> 
    Colombo was great.. 
    </div> 

$(function(){ 
$('body').on('click','button#printreceipt',function(e){ 
e.preventDefault(); 
e.stopPropagation(); 

var printWindow = window.open(''); 
var divContents = $("#receipt").html() + 
        "<script>" + 
        "window.onload = function() {" + 
        "  window.print();" + 
        "};" + 
        "setInterval(function() { { clearInterval();window.close(); } }, 300);" + 
        "<" + "/script>"; 
printWindow.document.write(divContents); 
printWindow.document.close(); 
}); 
}); 
0

這是什麼工作,我(在Chrome):

var html = "<html>...</html>"; 
var win = window.open(); 
win.document.write(html); 
win.document.close(); 
setTimeout(function() { 
    win.focus(); 
    win.print(); 
    win.close(); 
}, 100); 

關鍵位是做焦點()和print()異步。沒有這個,我在打印預覽中看到一個空白頁面。

相關問題