js window.open then print()

后端 未结 7 1886
深忆病人
深忆病人 2020-11-29 08:14

print() doesn\'t work in IE after opening a new window. It works in Chrome. Here\'s a tester:





        
相关标签:
7条回答
  • 2020-11-29 08:32

    What worked for me was adding myWindow.document.close() after myWindow.document.write(). Here's my solution with a timeout to wait for the new window to finish loading (if you have a lot to load):

    var win = window.open('', 'PrintWindow');
    win.document.write('Stuff to print...');
    
    setTimeout(function () {
        win.document.close();
        win.focus();
        win.print();
        win.close(); 
    }, 1000);
    
    0 讨论(0)
  • 2020-11-29 08:33
    <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" />
    
    0 讨论(0)
  • 2020-11-29 08:39
    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();
    }
    
    0 讨论(0)
  • 2020-11-29 08:47

    try this

    <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>
    
    0 讨论(0)
  • 2020-11-29 08:50

    checkout: window.print() not working in IE

    Working sample: http://jsfiddle.net/Q5Xc9/1/

    0 讨论(0)
  • 2020-11-29 08:52

    Turgut gave the right solution. Just for clarity, you need to add close after writing.

    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(); 
      }
    
    0 讨论(0)
提交回复
热议问题