谷歌浏览器打印预览不会第一次加载页面 [英] google chrome print preview does not load the page the first time

查看:272
本文介绍了谷歌浏览器打印预览不会第一次加载页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 < html> 
< head>
< script type =text / javascript>
函数Popup()
{

var mywindow = window.open('','Ticket info','height = 400,width = 600');
mywindow.document.write('< html>< head>< title>我的div< / title>');
mywindow.document.write('< style type =text / css> * {margin:0; padding:0;} body {padding:3px; padding-left:20px; font:6px bold Arial字体;}< /风格>');
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();
返回true;
}
< / script>
< / head>
< body>
< input type =buttonvalue =Print Divonclick =Popup(); />
< / body>
< / html>

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









我认为问题来自这一行:



$ p $ 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(){//等待所有资源加载
mywindow.document.close(); //对于IE需要> = 10
mywindow.focus(); //必要的IE> = 10
mywindow.print(); //将窗口改为winPrint
mywindow.close(); //改变窗口赢得打印
},250);
} else {
mywindow.document.close(); //必须为IE> = 10
mywindow.focus(); //必须为IE> = 10

mywindow.print();
mywindow.close();
}

返回true;
}


I'm trying to print a page using this code

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

Basically it will pop out a window and show a print preview of the page. the first attempt to load the print preview will not load the barcode and when you cancel the first print preview then right click the page and print again the 2nd print preview will now show the barcode to print.

I think the issue is coming from this line:

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

when I comment this line and add a dummy text to the page. It will automatically show up in the print preview on the first attempt.

I had the same issue before when I'm trying to load the style from a css file. I resolve this by transferring the styles directly to the popup window.

My question is why is this happening? and how can I load the barcode on the first attempt of the print preview?

解决方案

You need to put delay before print. There is a native defect in chrome.

Code would as under :-

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

这篇关于谷歌浏览器打印预览不会第一次加载页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆