2012-03-24 300 views
20

打開新窗口後,print()在IE中不起作用。它適用於Chrome。 這裏有一個testerjs window.open然後打印()

<html> 
<head> 
<script type="text/javascript"> 
    function openWin() 
    { 
    myWindow=window.open('','','width=200,height=100'); 
    myWindow.document.write("<p>This is 'myWindow'</p>"); 
    myWindow.focus(); 
    myWindow.print(); //DOES NOT WORK 
    } 
</script> 
</head> 
<body> 

<input type="button" value="Open window" onclick="openWin()" /> 

</body> 
</html> 

回答

2

試試這個

<html> 
<head> 
<script type="text/javascript"> 
function openWin() 
{ 
myWindow=window.open('','','width=200,height=100'); 
myWindow.document.write("<p>This is 'myWindow'</p>"); 
myWindow.focus(); 
print(myWindow); 
} 
</script> 
</head> 
<body> 

<input type="button" value="Open window" onclick="openWin()" /> 

</body> 
</html> 
+1

,在IE-11工作對我來說唯一的解決辦法。謝謝 !! – user3340627 2015-09-21 08:52:03

+0

在我的Chrome上,即使在關閉打印窗口後,窗口仍然保持打開狀態 – Miguel 2016-05-23 12:56:58

12

圖爾古特了合適的解決方案。爲了清楚起見,您需要在寫完後添加關閉。

function openWin() 
    { 
    myWindow=window.open('','','width=200,height=100'); 
    myWindow.document.write("<p>This is 'myWindow'</p>"); 


    myWindow.document.close(); //missing code 


    myWindow.focus(); 
    myWindow.print(); 
    } 
3
<script type="text/javascript"> 

    function printDiv(divName) { 
     var printContents = document.getElementById(divName).innerHTML; 
     var originalContents = document.body.innerHTML; 
     document.body.innerHTML = printContents; 
     window.print(); 
     document.body.innerHTML = originalContents; 
    } 

</script> 


<div id="printableArea">CONTENT TO PRINT</div> 



<input type="button" onclick="printDiv('printableArea')" value="Print Report" /> 
3
function printCrossword(printContainer) { 
    var DocumentContainer = getElement(printContainer); 
    var WindowObject = window.open('', "PrintWindow", "width=5,height=5,top=200,left=200,toolbars=no,scrollbars=no,status=no,resizable=no"); 
    WindowObject.document.writeln(DocumentContainer.innerHTML); 
    WindowObject.document.close(); 
    WindowObject.focus(); 
    WindowObject.print(); 
    WindowObject.close(); 
} 
相關問題