2012-03-24 16 views
20

print() nie działa w IE po otwarciu nowego okna. Działa w Chrome. Oto tester:js window.open then print()

<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> 

Odpowiedz

2

spróbować tej

<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

Jedynym rozwiązaniem, że pracował dla mnie w IE-11. Dzięki !! – user3340627

+0

Na moim chromie, okno pozostaje otwarte nawet po zamknięciu okna drukowania. – Miguel

12

Turgut dawało prawo rozwiązania. Dla jasności, musisz dodać blisko po napisaniu.

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