Chrome print function no longer works

Our website has a feature that allows you to print a member profile. The way it works is that the javascript function is attached to the button via onsubmit. The javascript function uses window.open to reopen the page in a special mode that updates the printable version of the page. A.

This functionality has existed since 2008 and works in all browsers. After about a week or so, it stops working in Chrome. In Chrome, what happens is that an open window opens, but then a small empty window opens, and then they all close.

In search of a discussion of this problem, I could not find the exact problem, but found something that said that onsubmit should add "return false". I tried to add this, but that did not help.

Here is what onsubmit looks like:

<button onclick="PrintEmailSubmit('print');">Print Profile</button> 

Here is the code that opens the window:

 window.open('print-email-profile.php?mode=' + mode,'','width=' + width + ',height=' + height + ',scrollbars=yes,location=0,menubar=1,status=0,toolbar=0') 

While there is no need to see, here is the whole PrintEmailSubmit () function:

 /* * called by view-profile.php */ function PrintEmailSubmit(mode) { var width; var height; switch(mode) { case 'print': width = 850; height = 1000; break; case 'email': width = 400; height = 120; break; default: alert('Error: invalid calling sequence -- should not happen!'); exit; } window.open('print-email-profile.php?mode=' + mode,'','width=' + width + ',height=' + height + ',scrollbars=yes,location=0,menubar=1,status=0,toolbar=0'); } 

And finally, what makes this work so that the special version of the page has the following body tag:

 <body onload="window.print();window.close();"> 

As stated above, the function continues to work in IE and Firefox. This problem occurs with Chrome.

Any ideas?

+4
source share
4 answers

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

The problem is that Chrome is looking for user input before printing. Window.print () opens the print dialog, but Chrome does not wait for printing to complete. Window.close () closes the print dialog and everything else in the parent.

To save you time, keep in mind that the OnAfterPrint hook is not used by Chrome at all. I also tried putting window.close () in onLoad and window.print () in onBeforeUnload, but the print dialog cancels window.close (). The next best 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 have not tested this, but I think it demonstrates the idea quite effectively.

+5
source

I found this solution and it really works:

 <body onload="window.print();window.onmouseover = function() { self.close(); } "> 
+2
source

Chrome runs so fast that it actually prints before loading a document. Therefore, the best thing to do is simply move the print function to onload, 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 oc, it does not work in IE, so the 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(); } 
+1
source

I am using Mrbresleveloper 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(); } 
+1
source

Source: https://habr.com/ru/post/1497100/


All Articles