I currently use the latest version of Chrome (43.0.2357.130) and experience a difference in print functionality when I call print from window.print() vs using βP.
When printing using window.print() it prints to the console correctly. It displays Before Print when the print dialog opens, and After Print when the dialog is closed.
However, when using the Chrome or βP menu to print, it registers both Before Print and After Print on the console when the print dialog box opens. A.
Here is the code I'm using that works well in other browsers.
function beforePrint() { console.log('Before Print'); } function afterPrint() { console.log('After Print'); } if (window.matchMedia) { var mediaQueryList = window.matchMedia('print'); mediaQueryList.addListener(function (mql) { (mql.matches) ? beforePrint() : afterPrint(); }); } else {
Once again for clarity, as if it were a console:
This is the expected result:
> window.print() Before Print // Print Dialog is now open, I press cancel to close dialog After Print
This is the result that I get when I use βP or the menu to start printing:
Before Print After Print
Is this a bug in Chrome, or is there another way to fix the event correctly?
For reference: Can I use the support table for matchMedia
source share