Print function in Chrome no longer working

前端 未结 6 1433
感情败类
感情败类 2020-12-03 19:36

Our website has a feature whereby a member profile can be printed. The way that it works is that a javascript function is attached to a button via an onsubmit. The javascrip

相关标签:
6条回答
  • 2020-12-03 19:49

    The button and the window.open really have nothing to do with your problem.

    The problem is, Chrome is looking for user input before it prints. Window.print() opens the print dialog window, but Chrome isn't waiting for you to finish printing. The window.close() is closing the print dialog window and everything else in the parent.

    In an effort to save you time, be aware that the OnAfterPrint hook isn't used by Chrome at all. I also tried putting window.close() in the onLoad and window.print() in the onBeforeUnload, but the print dialog cancels the window.close(). The next best thing would be to do something like:

    //In your profile print page
    <html>
    <head>
    <script>
    var is_chrome = function () { return Boolean(window.chrome); }
    if(is_chrome) 
    {
       window.print();
       setTimeout(function(){window.close();}, 10000); 
       //give them 10 seconds to print, then close
    }
    else
    {
       window.print();
       window.close();
    }
    </script>
    
    <body onLoad="loadHandler();">
    

    I haven't tested this, but I think it demonstrates the idea fairly effectively.

    0 讨论(0)
  • 2020-12-03 19:50
    //In your profile print page
    <html>
    <head>
    <script>
    var is_chrome = function () { return Boolean(window.chrome); }
    if(is_chrome) 
    {
       window.print();
       setTimeout(function(){window.close();}, 10000); 
       //give them 10 seconds to print, then close
    }
    else
    {
       window.print();
       window.close();
    }
    </script>
    
    <body onLoad="loadHandler();">
    

    This works fine.

    Thanks.

    0 讨论(0)
  • 2020-12-03 19:52

    The Chrome is so fast that it actually call the print before the document is loaded. That's why the best thing to do is just moving the print function to onload, either like Mari or like this:

    winPrint = window.open("", "winPrint", "width=1,height=1");
    winPrint.document.write(html);
    winPrint.onload = function () {
        window.print();
        window.close();
    };
    

    And o.c. it's not working in IE so full code should look like this

    var is_chrome = Boolean(window.chrome);
    if (is_chrome) {
        winPrint.onload = function () {
            window.print();
            window.close();
        };
    }
    else {
        winPrint.print();
        winPrint.close();
    }
    
    0 讨论(0)
  • 2020-12-03 19:57

    I use Mr.bresleveloper solution with some changes:

    var is_chrome = Boolean(window.chrome);
    if (is_chrome) {
        winPrint.onload = function () {
            setTimeout(function () { // wait until all resources loaded 
                winPrint.print();  // change window to winPrint
                winPrint.close();// change window to winPrint
            }, 200);
        };
    }
    else {
        winPrint.print();
        winPrint.close();
    }
    
    0 讨论(0)
  • 2020-12-03 20:02

    I found this solution and it really works:

    <body onload="window.print();window.onmouseover = function() { self.close(); } ">
    
    0 讨论(0)
  • 2020-12-03 20:11

    I also get the same problem with this methods

       window.print();
       window.close();
    

    even recognizing the browser and then executing the code accordingly is good. But this is a very handy and short cut solution to fix this issue only by two line.

       document.execCommand('print');
       window.close();
    

    working perfectly fine in chrome and IE

    0 讨论(0)
提交回复
热议问题