[英]google chrome print preview does not load the page the first time
我正在嘗試使用此代碼打印頁面
<html>
<head>
<script type="text/javascript">
function Popup()
{
var mywindow = window.open('', 'Ticket info', 'height=400,width=600');
mywindow.document.write('<html><head><title>my div</title>');
mywindow.document.write('<style type="text/css"> *{margin: 0; padding: 0;} body{padding: 3px; padding-left:20px;font:6px bold Arial;}</style>');
mywindow.document.write('<script src="http://code.jquery.com/jquery-latest.min.js"><'+'/script>');
mywindow.document.write('<script src="jquery-barcode.min.js"><'+'/script>');
mywindow.document.write('</head><body>');
mywindow.document.write('<div id="demo"></div>');
mywindow.document.write('<script type="text/javascript">$("#demo").barcode("1234567890128", "code39");<'+'/script>');
mywindow.document.write('</body></html>');
mywindow.print();
return true;
}
</script>
</head>
<body>
<input type="button" value="Print Div" onclick="Popup();" />
</body>
</html>
基本上它會彈出一個窗口並顯示頁面的打印預覽。 加載打印預覽的第一次嘗試將不會加載條形碼,當您取消第一次打印預覽時,然后右鍵單擊頁面並再次打印第二次打印預覽現在將顯示要打印的條形碼。
我認為問題來自這條線:
mywindow.document.write('<script type="text/javascript">$("#demo").barcode("1234567890128", "code39");<'+'/script>');
當我評論此行並向頁面添加虛擬文本時。 它將在第一次嘗試時自動顯示在打印預覽中。
在我嘗試從css文件加載樣式之前,我遇到了同樣的問題。 我通過將樣式直接傳遞到彈出窗口來解決這個問題。
我的問題是為什么會發生這種情況? 如何在首次嘗試打印預覽時加載條形碼?
你需要在打印前放置延遲。 鉻有一種天然缺陷。
代碼如下: -
function PrintDiv(data) {
var mywindow = window.open();
var is_chrome = Boolean(mywindow.chrome);
mywindow.document.write(data);
if (is_chrome) {
setTimeout(function() { // wait until all resources loaded
mywindow.document.close(); // necessary for IE >= 10
mywindow.focus(); // necessary for IE >= 10
mywindow.print(); // change window to winPrint
mywindow.close(); // change window to winPrint
}, 250);
} else {
mywindow.document.close(); // necessary for IE >= 10
mywindow.focus(); // necessary for IE >= 10
mywindow.print();
mywindow.close();
}
return true;
}
這對我有用(以及實現Anand觀察到的IE 10+要求):
function Popup() {
var _window = window.open('');
_window.document.write(data);
// required for IE >= 10
_window.document.close();
_window.focus();
_window.document.body.onload = function() {
// continue to print
_window.print();
_window.close();
};
return true;
}
而不是等待窗口加載並嘗試過早地打印它,這只是等待整個document.body
加載。
我在自動彈出窗口中移動了window.print()命令,因此它可以在打印之前首先加載jquery函數。
這是我添加的行。
mywindow.document.write('<script type="text/javascript">window.print();<'+'/script>');
這是整個代碼:
<html>
<head>
<script type="text/javascript">
function Popup()
{
var mywindow = window.open('', 'Ticket info', 'height=400,width=600');
mywindow.document.write('<html><head><title>my div</title>');
mywindow.document.write('<style type="text/css"> *{margin: 0; padding: 0;} body{padding: 3px; padding-left:20px;font:6px bold Arial;}</style>');
mywindow.document.write('<script src="http://code.jquery.com/jquery-latest.min.js"><'+'/script>');
mywindow.document.write('<script src="jquery-barcode.min.js"><'+'/script>');
mywindow.document.write('</head><body>');
mywindow.document.write('<div id="demo"></div>');
mywindow.document.write('<script type="text/javascript">$("#demo").barcode("1234567890128", "code39");<'+'/script>');
mywindow.document.write('<script type="text/javascript">window.print();<'+'/script>');
mywindow.document.write('</body></html>');
return true;
}
</script>
</head>
<body>
<input type="button" value="Print Div" onclick="Popup();" />
</body>
</html>
實際上你可以創建一個擴展css並將你所有的css放在那里......
默認
mywindow.document.write('<link rel="stylesheet" type="text/css" href="/css/default.css"/>');
然后就這樣替換它
mywindow.document.write('<link rel="stylesheet" type="text/css" href="/css/default.css" media="print"/>');
只需添加media ='print'即可識別它..
希望它會有所幫助......
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.