[英]window.print() not working in IE
我在javascript中做這樣的事情,點擊鏈接打印我的頁面的一部分
function printDiv() {
var divToPrint = document.getElementById('printArea');
var newWin = window.open();
newWin.document.write(divToPrint.innerHTML);
newWin.print();
newWin.close();
}
它在Firefox中很好用,但在IE中卻不行。
請有人幫忙
在newWin.document.write(divToPrint.innerHTML)
之后添加這些行
newWin.document.close();
newWin.focus();
newWin.print();
newWin.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();
}
這讓IE很開心。 HTH,-Ted
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(),而不是從窗口實例調用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();
}
為onload添加檢查條件
if (newWinObj.onload) {
newWinObj.onload = function() {
newWinObj.print();
newWinObj.close();
};
}
else {
newWinObj.print();
newWinObj.close();
}
只是添加一些額外的信息。 在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>
只需等一段時間關閉窗口!
if (navigator.appName != 'Microsoft Internet Explorer') {
newWin.close();
} else {
window.setTimeout(function() {newWin.close()}, 3000);
}
我被告知在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();
我不確定,但我認為這是因為InternetExplorer的安全規則...
如果你調用print()這樣的函數,它會手動詢問用戶是否要允許活動腳本,如果他點擊黃色欄並選擇“是”,則會出現打印對話框。 如果單擊“否”或者不執行任何操作,則不會執行被視為活動腳本或其他安全相關的javascript函數的部分。
在您的示例中,窗口打開然后調用print(),彈出確認欄(沒有選擇任何內容,實際上由於時間短而無法選擇任何內容),調用newWin.close(),窗口關閉。
您應該嘗試將該頁面添加到InternetExplorer中的受信任站點或更改安全設置。
可能有一種方法可以在javascript本身中處理安全策略,但我對InternetExplorer安全策略知之甚少。
希望這可以幫助
我們通常處理打印的方式是打開新窗口,其中包含需要發送到打印機的所有內容。 然后我們讓用戶實際點擊他們的瀏覽器打印按鈕。
這在過去一直是可以接受的,它避開了Chilln所談論的安全限制。
這對我有用,它適用於firefox,即chrome和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();
適用於Firefox
iframewin.print()
用於IE
iframedocument.execCommand('print', false, null);
只有當它不是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
function functionname() {
var divToPrint = document.getElementById('divid');
newWin= window.open();
newWin.document.write(divToPrint.innerHTML);
newWin.location.reload();
newWin.focus();
newWin.print();
newWin.close();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.