Print event detection in browser occurs in Chrome

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 { // basically a fallback for < IE11 window.addEventListener('beforeprint', beforePrint, false); window.addEventListener('afterprint', afterPrint, false); } 

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 // Print Dialog is now open, but it is occurring in the wrong place 

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

+6
source share
1 answer

It seems like this is most likely a bug with Chrome. https://bugs.chromium.org/p/chromium/issues/detail?id=422883

+1
source

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


All Articles