window.print() not working in IE
我在javascript中做这样的事情,点击链接即可打印页面的一部分
1 2 3 4 5 6 7 | function printDiv() { var divToPrint = document.getElementById('printArea'); var newWin = window.open(); newWin.document.write(divToPrint.innerHTML); newWin.print(); newWin.close(); } |
它在Firefox中效果很好,但在IE中效果不好。
有人可以帮忙吗
在
1 2 3 4 | newWin.document.close(); newWin.focus(); newWin.print(); newWin.close(); |
然后打印功能将在所有浏览器中起作用...
添加newWin.document.close();,如下所示:
1 2 3 4 5 6 7 8 | function printDiv() { var divToPrint = document.getElementById('printArea'); var newWin = window.open(); newWin.document.write(divToPrint.innerHTML); newWin.document.close(); newWin.print(); newWin.close(); } |
这使IE感到高兴。
HTH,
-泰德
1 2 3 4 5 6 7 8 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(); } |
我以前遇到过这个问题,解决方案就是在IE中调用window.print(),而不是从window实例调用print:
1 2 3 4 5 6 7 | function printPage(htmlPage) { var w = window.open("about:blank"); w.document.write(htmlPage); if (navigator.appName == 'Microsoft Internet Explorer') window.print(); else w.print(); } |
请稍等片刻,然后再关闭窗口!
1 2 3 4 5 | if (navigator.appName != 'Microsoft Internet Explorer') { newWin.close(); } else { window.setTimeout(function() {newWin.close()}, 3000); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | <!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> Do not print Print this div <button onClick="printdiv();">Print Div</button> </body> 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 onloadx="window.print()">' + '' + printContents + ''+'</body>'+'</html>'); popupWin.document.close(); return false; }; </html> |
添加onload的检查条件
1 2 3 4 5 6 7 8 9 10 | if (newWinObj.onload) { newWinObj.onload = function() { newWinObj.print(); newWinObj.close(); }; } else { newWinObj.print(); newWinObj.close(); } |
只是添加一些其他信息。在IE 11中,仅使用
1 | window.open() |
原因
1 | window.document |
未定义。要解决此问题,请使用
1 | window.open( null, '_blank' ) |
这也将在Chrome,Firefox和Safari中正常运行。
我没有足够的声誉来发表评论,因此不得不创建一个答案。
这对我有用,它在Firefox(即chrome)中工作。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | var content ="This is a test Message"; var contentHtml = [ '', 'TestReport', '<button style="float:right; margin-right:10px;"', 'id="printButton" onclick="printDocument()">Print</button>', content ].join(''); var printWindow = window.open(); printWindow.document.write('<!DOCTYPE HTML><html><headReports', 'function printDocument() {', 'window.focus();', 'window.print();', 'window.close();', '}', ''); 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(); |
我不确定,但是我认为它是由于InternetExplorer的安全规则而发生的...
如果调用诸如print()之类的函数,它将手动询问用户是否要允许活动脚本,如果他单击黄色栏并选择"是",则会出现打印对话框。如果您单击"否"或只是不执行任何操作,则说明它没有执行被视为活动脚本或其他与安全相关的javascript函数的部分。
在您的示例中,打开了窗口,然后调用了print(),弹出确认栏(未选择任何内容,由于时间短,实际上什么也不能选择),调用newWin.close(),关闭窗口。
您应该尝试将页面添加到InternetExplorer中的受信任站点或更改安全设置。
javascript中可能有一种处理安全策略的方法,但我对InternetExplorer安全策略了解不多。
希望这可以帮助
对于Firefox使用
1 | iframewin.print() |
供IE使用
1 | iframedocument.execCommand('print', false, null); |
另请参见无法使用JavaScript在IE上打印iframe,而是打印父页面
仅在不是IE时关闭窗口:
1 2 3 4 5 6 7 | 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(); } |
我们通常处理打印的方法是只打开新窗口,其中包含需要发送到打印机的所有内容。然后,我们让用户实际单击其浏览器的"打印"按钮。
在过去,这一直是可以接受的,它回避了Chilln谈论的安全限制。
有人告诉我在document.write之后执行document.close,但我不知道如何或为什么这样做,但是这导致我的脚本要等到我关闭打印对话框后,才能运行window.close。
1 2 3 4 5 6 7 8 9 | 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(); |
我也面临这个问题。
IE中的问题是
newWin.document.write(divToPrint.innerHTML);
当我们在IE中删除此行打印功能时。但是关于页面内容的问题仍然存在。
您可以使用window.open打开页面,然后在该页面中写入内容。然后打印功能将在IE中起作用。这是替代解决方案。
好运。
@Pratik
1 2 3 4 5 6 7 8 9 10 | function functionname() { var divToPrint = document.getElementById('divid'); newWin= window.open(); newWin.document.write(divToPrint.innerHTML); newWin.location.reload(); newWin.focus(); newWin.print(); newWin.close(); } |