【问题标题】:window.print() not working in IEwindow.print() 在 IE 中不起作用
【发布时间】:2011-02-03 02:01:03
【问题描述】:

我在 javascript 中做这样的事情来打印我的页面的一部分点击链接

function printDiv() {
 var divToPrint = document.getElementById('printArea');
 var newWin = window.open();
 newWin.document.write(divToPrint.innerHTML);
 newWin.print();
 newWin.close();
}

它在 Firefox 中运行良好,但在 IE 中不行。

有人可以帮忙

【问题讨论】:

    标签: javascript internet-explorer printing


    【解决方案1】:

    newWin.document.write(divToPrint.innerHTML)之后添加这些行

    newWin.document.close();
    newWin.focus();
    newWin.print();
    newWin.close();
    

    然后打印功能将在所有浏览器中工作......

    【讨论】:

    • 非常感谢。原因是缺少 document.close();
    • 效果很好,Chrome 将这种方法识别为弹出窗口并默认阻止它
    • 这对我有用,谢谢。记住也要使用第一行 window.document.close();否则它不起作用,对我来说,我的意思是......
    • 在 Chrome 61 中,浏览器会立即关闭打印窗口。删除行 newWin.close() 后,它在 chrome 中运行良好......现在用户必须手动关闭窗口
    • @Pratik 您好,感谢您的解决方案。我有同样的问题,但我没有打开新窗口进行打印。如何使用关闭()?请你帮助我好吗?我确实为此提出了一个问题stackoverflow.com/questions/49410902 谢谢
    【解决方案2】:

    添加newWin.document.close();,像这样:

    function printDiv() {
       var divToPrint = document.getElementById('printArea');
       var newWin = window.open();
       newWin.document.write(divToPrint.innerHTML);
       newWin.document.close();
       newWin.print();
       newWin.close();
    }
    

    这让 IE 很高兴。 高温下, -泰德

    【讨论】:

      【解决方案3】:
      function printDiv() {
          var divToPrint = document.getElementById('printArea');
          newWin= window.open();
          newWin.document.write(divToPrint.innerHTML);
          newWin.location.reload();
          newWin.focus();
          newWin.print();
          newWin.close();
      }
      

      【讨论】:

        【解决方案4】:

        我之前也遇到过这个问题,解决方法就是在IE中调用window.print(),而不是从window实例调用print:

        function printPage(htmlPage)
            {
                var w = window.open("about:blank");
                w.document.write(htmlPage);
                if (navigator.appName == 'Microsoft Internet Explorer') window.print();
                else w.print();
            }
        

        【讨论】:

        【解决方案5】:

        请稍等片刻再关闭窗口!

        if (navigator.appName != 'Microsoft Internet Explorer') {
            newWin.close();
        } else {
            window.setTimeout(function() {newWin.close()}, 3000);
        }
        

        【讨论】:

          【解决方案6】:

          为onload添加检查条件

          if (newWinObj.onload) {
              newWinObj.onload = function() {
                  newWinObj.print();
                  newWinObj.close();
              };
          }
          else {
              newWinObj.print();
              newWinObj.close();
          }
          

          【讨论】:

            【解决方案7】:

            只是添加一些额外的信息。在 IE 11 中,仅使用

            window.open() 
            

            原因

            window.document
            

            未定义。要解决此问题,请使用

            window.open( null, '_blank' )
            

            这也可以在 Chrome、Firefox 和 Safari 中正常工作。

            我没有足够的声誉来发表评论,所以不得不创建一个答案。

            【讨论】:

              【解决方案8】:
              <!DOCTYPE html>
              <html>
              <head id="head">
              <meta http-equiv="X-UA-Compatible" content="IE=9; IE=8; IE=7; IE=EDGE" /> 
                <!-- saved from url=(0023)http://www.contoso.com/ -->
                <link rel="stylesheet" type="text/css" href="style.css" />
              </head>
              <body>
              <div>
                <div>
                  Do not print
                </div>
                <div id="printable" style="background-color: pink">
                  Print this div
                </div>
                <button onClick="printdiv();">Print Div</button>
              </div>
              </body>
              <script>
                function printdiv()
                  {
                var printContents = document.getElementById("printable").innerHTML;
                var head = document.getElementById("head").innerHTML;
                //var popupWin = window.open('', '_blank');
                var popupWin = window.open('print.html', 'blank');
                popupWin.document.open();
                popupWin.document.write(''+ '<html>'+'<head>'+head+'</head>'+'<body onload="window.print()">' + '<div id="printable">' + printContents + '</div>'+'</body>'+'</html>');
                popupWin.document.close();
               return false;
              };
              </script>
              </html>
              

              【讨论】:

              • 文件名为print.html
              【解决方案9】:

              我不确定,但我认为这是因为 InternetExplorer 的安全规则...

              如果您调用 print() 之类的函数,它会手动询问用户是否要允许活动脚本,如果他单击黄色栏并选择“是”,则会出现打印对话框。如果您单击“否”或不执行任何操作,则不会执行被视为活动脚本或其他安全相关 javascript 函数的部分。

              在您的示例中,窗口打开,然后调用 print(),弹出确认栏(未选择任何内容,实际上由于时间短,无法选择任何内容),调用 newWin.close(),窗口关闭。

              您应该尝试将页面添加到 InternetExplorer 中的受信任站点或更改安全设置。

              可能有一种方法可以在 javascript 本身中处理安全策略,但我对 InternetExplorer 安全策略了解不多。

              希望对你有帮助

              【讨论】:

                【解决方案10】:

                我们通常处理打印的方式是打开新窗口,其中包含需要发送到打印机的所有内容。然后我们让用户实际点击浏览器的“打印”按钮。

                这在过去一直是可以接受的,它回避了 Chilln 所说的安全限制。

                【讨论】:

                  【解决方案11】:

                  只有在不是IE的时候才关闭窗口:

                  function printDiv() {
                   var divToPrint = document.getElementById('printArea');
                   var newWin= window.open();
                   newWin.document.write(divToPrint.innerHTML);
                   newWin.print();
                   if (navigator.appName != 'Microsoft Internet Explorer') newWin.window.close();
                  }
                  

                  【讨论】:

                    【解决方案12】:

                    有人告诉我在 document.write 之后执行 document.close,我不明白如何或为什么,但这导致我的脚本等到我关闭打印对话框后才运行我的 window.close。

                    var printContent = document.getElementbyId('wrapper').innerHTML;
                    var disp_setting="toolbar=no,location=no,directories=no,menubar=no, scrollbars=no,width=600, height=825, left=100, top=25"
                    var printWindow = window.open("","",disp_setting);
                    printWindow.document.write(printContent);
                    printWindow.document.close();
                    printWindow.focus();
                    printWindow.print();
                    
                    printWindow.close();
                    

                    【讨论】:

                      【解决方案13】:

                      这对我有用,它适用于 Firefox,即和 chrome。

                          var content = "This is a test Message";
                      
                          var contentHtml = [
                              '<div><b>',
                              'TestReport',
                              '<button style="float:right; margin-right:10px;"',
                              'id="printButton" onclick="printDocument()">Print</button></div>',
                              content
                          ].join('');
                      
                          var printWindow = window.open();
                      
                          printWindow.document.write('<!DOCTYPE HTML><html><head<title>Reports</title>',
                              '<script>function printDocument() {',
                              'window.focus();',
                              'window.print();',
                              'window.close();',
                              '}',
                              '</script>');
                      
                          printWindow.document.write("stylesheet link here");
                      
                          printWindow.document.write('</head><body>');
                          printWindow.document.write(contentHtml);
                          printWindow.document.write('</body>');
                          printWindow.document.write('</html>');
                          printWindow.document.close();
                      

                      【讨论】:

                        【解决方案14】:

                        火狐使用

                        iframewin.print()
                        

                        IE 使用

                        iframedocument.execCommand('print', false, null);
                        

                        另见Unable to print an iframe on IE using JavaScript, prints parent page instead

                        【讨论】:

                          【解决方案15】:

                          我也面临这个问题。

                          IE 中的问题是 newWin.document.write(divToPrint.innerHTML);

                          当我们删除此行时,IE 中的打印功能正在工作。但是关于页面内容的问题仍然存在。

                          您可以使用 window.open 打开页面,并在该页面中写入内容。然后打印功能将在 IE 中工作。这是替代解决方案。

                          祝你好运。

                          @Pratik

                          【讨论】:

                            【解决方案16】:
                            function functionname() {
                            
                                var divToPrint = document.getElementById('divid');
                                newWin= window.open();
                                newWin.document.write(divToPrint.innerHTML);
                                newWin.location.reload();
                                newWin.focus();
                                newWin.print();
                                newWin.close();
                            }
                            

                            【讨论】:

                              猜你喜欢
                              • 1970-01-01
                              • 2017-08-11
                              • 1970-01-01
                              • 1970-01-01
                              • 1970-01-01
                              • 1970-01-01
                              • 1970-01-01
                              • 2012-10-28
                              • 1970-01-01
                              相关资源
                              最近更新 更多