Javascript window.print()在IE中不工作

Javascript window.print()在IE中不工作,javascript,internet-explorer,printing,Javascript,Internet Explorer,Printing,我在javascript中做了类似的事情,在点击链接时打印页面的一部分 function printDiv() { var divToPrint = document.getElementById('printArea'); var newWin = window.open(); newWin.document.write(divToPrint.innerHTML); newWin.print(); newWin.close(); } 它在Firefox中工作得很好,但在IE中却不行

我在javascript中做了类似的事情,在点击链接时打印页面的一部分

function printDiv() {
 var divToPrint = document.getElementById('printArea');
 var newWin = window.open();
 newWin.document.write(divToPrint.innerHTML);
 newWin.print();
 newWin.close();
}
它在Firefox中工作得很好,但在IE中却不行


有人能帮忙吗?

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

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

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

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

javascript本身可能有一种处理安全策略的方法,但我对InternetExplorer安全策略知之甚少


希望这有帮助

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


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

仅当窗口不是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();
}

我也面临这个问题

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

当我们删除这行打印功能在IE中工作。但页面内容仍然存在问题

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

祝你好运


@Pratik

newWin.document.write(divToPrint.innerHTML)


然后print函数将在所有浏览器中工作…

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

等一会儿再关窗户

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

我被告知在document.write之后执行document.close,我不知道如何或为什么,但这导致我的脚本等待关闭打印对话框后才运行我的窗口。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();

添加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();
}
这让我很高兴。 嗯,,
-Ted

为空载添加检查条件

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

这对我来说很有效,在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();
var content=“这是一条测试消息”;
var contentHtml=[
'',
“测试报告”,
“打印”,
内容
].加入(“”);
var printWindow=window.open();
printWindow.document.write(“供Firefox使用

iframewin.print()
供IE使用

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

另请参见

以添加一些附加信息。在IE 11中,仅使用

window.open() 
原因

window.document
要解决此问题,请使用

window.open( null, '_blank' )
这在Chrome、Firefox和Safari中也能正常工作

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


<!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>
不要打印 打印这个div 印刷部 函数printdiv() { var printContents=document.getElementById(“可打印”).innerHTML; var head=document.getElementById(“head”).innerHTML; //var popupWin=window.open('',''u blank'); var popupWin=window.open('print.html','blank'); popupWin.document.open(); popupWin.document.write(“”+“”+“”+head+“”+“”+“”+“”+“”+打印内容+“”+“”+“”+“”); popupWin.document.close(); 返回false; };
您在将近一年前的回复对我有所帮助。我已将此解决方案w.r.t发布到我自己的Qs:。投票通过。非常感谢。缺少文档。关闭()原因是;工作得很好,Chrome将此方法识别为弹出窗口,并通过默认设置阻止它。这对我很有效,谢谢。记住也要使用第一行window.document.close();否则它不会工作,对我来说,我的意思是…在Chrome 61中,浏览器会立即关闭打印窗口。删除行
newWin.close()后
它在chrome中工作良好…现在用户必须关闭窗口manually@Pratik您好,谢谢您的解决方案。我也有同样的问题,但我没有打开新窗口进行打印。如何使用close()?您能帮助我吗?我确实为此打开了一个问题,谢谢文件名为print.html
window.document
window.open( null, '_blank' )
<!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>