在IE浏览器中,window.print()无法正常工作。

68

我正在使用JavaScript编写类似以下代码的功能,在单击链接时打印页面的某个部分:

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

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

有人可以帮忙吗?

16个回答

139
将以下代码添加在 newWin.document.write(divToPrint.innerHTML) 之后。
newWin.document.close();
newWin.focus();
newWin.print();
newWin.close();

那么打印函数将在所有浏览器中正常工作...


5
非常感谢。缺少 document.close() 是导致问题的原因; - Ahmad
工作得非常好,但Chrome将此方法识别为弹出窗口并默认阻止它。 - Beta033
1
这对我有效,谢谢。记得也使用第一行代码 window.document.close(); 否则对我来说不起作用... - Matteo Bononi 'peorthyr'
@Pratik您好,感谢您的解决方案。我有同样的问题,但我不想打开一个新窗口来打印。我应该如何使用close()函数?您能帮我吗?我已经在Stack Overflow上发布了这个问题https://stackoverflow.com/questions/49410902 谢谢 - codeispoetry
popupWindowRef.document.close(); 这是我诊断过的最令人沮丧的问题之一。我花了3个小时才放弃并搜索找到这个问答。我应该更早地搜索。明显,IE11和FireFox需要这个,但Chrome或Edge不需要???还是感谢您!! - TetraDev
显示剩余2条评论

12

加入 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感到满意。 祝好, -Ted


9
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()即可,而不是从窗口实例中调用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();
    }

你在一年前的回复帮了我很多。我已经将这个解决方案发布到我的问题中:https://dev59.com/EWzXa4cB1Zd3GeqPRkKu#13948819。已点赞。 - The Dark Knight

3

在关闭窗口之前,请等待一段时间!

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

3
只是提供一些额外的信息。在IE 11中,仅使用
window.open() 

原因
window.document

出现未定义的情况。要解决此问题,请使用

window.open( null, '_blank' )

这也适用于Chrome、Firefox和Safari浏览器。

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


3

为onload添加检查条件

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

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

1
文件名为print.html。 - Luyao Chang

2

这对我有用,它在Firefox、IE和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();

2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接