[英]Window.Print() not working in chrome/Firefox
我正在使用以下代碼打印 div 中的內容。 它在 IE 中運行良好,但在 chrome 和 firefox 中顯示空白屏幕。
代碼:
var DocumentContainer = document.getElementById('TermsMainDiv');
var WindowObject = window.open('', "PrintWindow",
"width=800,height=700,top=200,left=200,toolbars=no,scrollbars=yes,status=no,resizable=no");
WindowObject.document.writeln(DocumentContainer.innerHTML);
WindowObject.document.close();
WindowObject.focus();
WindowObject.print();
WindowObject.close();
IE 和其他瀏覽器的單獨代碼。 它現在工作正常。
代碼:
<script type='text/javascript'>
var originalContents;
function Print() {
if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) {
var DocumentContainer = document.getElementById('TermsSCMainDiv');
var WindowObject = window.open('', "PrintWindow",
"width=800,height=700,top=200,left=200,toolbars=no,
scrollbars=yes,status=no,resizable=no");
WindowObject.document.writeln(DocumentContainer.innerHTML);
WindowObject.document.close();
WindowObject.focus();
WindowObject.print();
WindowObject.close();
}
else {
originalContents = document.body.innerHTML;
var printable = document.getElementById('TermsSCMainDiv');
document.body.innerHTML = printable.innerHTML;
printCoupon();
}
}
function printCoupon() {
window.print();
endPrintCoupon();
}
function endPrintCoupon() {
document.body.innerHTML = originalContents;
document.getElementById('TermsSCMainDiv').scrollIntoView(true);
location.reload();
}
</script>
這似乎也有效:
WindowObject.document.writeln(html);
WindowObject.document.close();
WindowObject.scrollTo(0,0);
WindowObject.focus();
WindowObject.print();
當我發現注釋掉 .print() 函數會導致彈出頁面的正確呈現時發現了這一點。 scrollTo 觸發通常被頁面前面的打印對話框阻止的呈現。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.