【问题标题】:js window.open then print()js window.open 然后 print()
【发布时间】:2012-04-08 18:42:57
【问题描述】:

print() 在打开新窗口后在 IE 中不起作用。它适用于 Chrome。 这是tester

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

【问题讨论】:

    标签: javascript html printing window


    【解决方案1】:
    【解决方案2】:

    Turgut 给出了正确的解决方案。为了清楚起见,您需要在编写后添加关闭。

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

      【讨论】:

        【解决方案4】:
        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();
        }
        

        【讨论】:

          【解决方案5】:

          对我有用的是在myWindow.document.write() 之后添加myWindow.document.close()。这是我的解决方案,超时等待新窗口完成加载(如果你有很多要加载的话):

          var win = window.open('', 'PrintWindow');
          win.document.write('Stuff to print...');
          
          setTimeout(function () {
              win.document.close();
              win.focus();
              win.print();
              win.close(); 
          }, 1000);
          

          【讨论】:

          • 它确实有效,但找不到为什么 win.document.close() 需要它。
          【解决方案6】:

          试试这个

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

          【讨论】:

          • 在 IE-11 中唯一对我有用的解决方案。谢谢!!
          • 在我的 chrome 上,即使关闭打印窗口,窗口仍保持打开状态
          【解决方案7】:

          由于大多数浏览器已经更新,所以打印和关闭不再像以前那样工作了。所以你应该添加 onafterprint 事件监听器来关闭打印窗口。

              var printWindow = window.open('https://stackoverflow.com/');
              printWindow.print();
          
              //Close window once print is finished
              printWindow.onafterprint = function(){
                 printWindow.close()
              };
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-01-30
            • 1970-01-01
            • 2013-01-12
            • 1970-01-01
            • 2015-02-20
            相关资源
            最近更新 更多