【问题标题】:google chrome print preview does not load the page the first time谷歌浏览器打印预览第一次没有加载页面
【发布时间】:2015-04-05 07:05:23
【问题描述】:

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

<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 文件加载样式之前,我遇到了同样的问题。我通过将样式直接转移到弹出窗口来解决这个问题。

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

【问题讨论】:

    标签: javascript jquery google-chrome printing


    【解决方案1】:

    您需要在打印之前设置延迟。 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;
    }
    

    【讨论】:

    • 你救了我的命,伙计
    • 感谢@HaiderAliWajihi 的赞赏。您可以喜欢我的回答,以便其他人也可以找到此解决方案
    【解决方案2】:

    这对我有用(以及实现 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 加载。

    【讨论】:

    • 这对我来说比首选答案(在 Chrome 上测试)更好,后者仅在第一次有效,而在以后无效。
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      实际上你可以创建一个扩展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' 即可识别它..

      希望对您有所帮助...

      【讨论】:

        猜你喜欢
        • 2013-09-15
        • 2011-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-04
        • 2015-10-21
        • 1970-01-01
        相关资源
        最近更新 更多