gpt4 book ai didi

javascript - 谷歌浏览器打印预览第一次不加载页面

转载 作者:数据小太阳 更新时间:2023-10-29 05:41:17 28 4
gpt4 key购买 nike

我正在尝试使用此代码打印页面

<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>

基本上它会弹出一个窗口并显示页面的打印预览。第一次尝试加载打印预览不会加载条形码,当您取消第一次打印预览然后右键单击页面并再次打印时,第二次打印预览现在将显示要打印的条形码。

1st print attempt

reprint

2nd print attempt

我认为问题出在这一行:

mywindow.document.write('<script type="text/javascript">$("#demo").barcode("1234567890128", "code39");<'+'/script>');

当我评论这一行并向页面添加虚拟文本时。它会在第一次尝试时自动显示在打印预览中。

我之前在尝试从 css 文件加载样式时遇到过同样的问题。我通过将样式直接转移到弹出窗口来解决这个问题。

我的问题是为什么会这样?以及如何在第一次尝试打印预览时加载条形码?

最佳答案

您需要在打印前延迟。chrome 有一个原生缺陷。

代码如下:-

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;
}

关于javascript - 谷歌浏览器打印预览第一次不加载页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28343748/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com