Javascript window.print() 在 IE 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2555697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:53:47  来源:igfitidea点击:

window.print() not working in IE

javascriptinternet-explorerprinting

提问by Pankaj

I am doing something like this in javascript to print a section of my page on click of a link

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

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

It works great in Firefox but not in IE.

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

Could someone please help

有人可以帮忙吗

回答by Pratik

Add these lines after newWin.document.write(divToPrint.innerHTML)

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

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

Then print function will work in all browser...

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

回答by Ted

Add newWin.document.close();, like so:

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

This makes IE happy. HTH, -Ted

这让 IE 高兴。HTH, -Ted

回答by sarkiroka

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

回答by Kyle

I've had this problem before, and the solution is simply to call window.print() in IE, as opposed to calling print from the window instance:

我以前也遇到过这个问题,解决办法就是在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();
    }

回答by Greg

Just wait some time before closing the window!

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

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

回答by Derin

add checking condition for onload

为 onload 添加检查条件

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

回答by Clinton Chau

Just to add some additional information. In IE 11, using merely

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

window.open() 

causes

原因

window.document

to be undefined. To resolve this, use

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

window.open( null, '_blank' )

This will also work correctly in Chrome, Firefox and Safari.

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

I don't have enough reputation to comment, so had to create an answer.

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

回答by Luyao Chang

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

回答by Michael G?rtner

For Firefox use

供 Firefox 使用

iframewin.print()

for IE use

供 IE 使用

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

see also Unable to print an iframe on IE using JavaScript, prints parent page instead

另请参阅无法使用 JavaScript 在 IE 上打印 iframe,改为打印父页面

回答by Nayas Subramanian

This worked for me, it works in firefox, ie and chrome.

这对我有用,它适用于 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();