在Chrome中发现浏览器打印事件有两种不同的方式 [英] Detecting browser print event happens two different ways in Chrome

查看:787
本文介绍了在Chrome中发现浏览器打印事件有两种不同的方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用最新版本的Chrome(43.0.2357.130),并且在从 window.print()调用打印时遇到打印功能上的差异

使用 window.print()打印时,它会正确输出到控制台。它在打印对话框打开时输出打印前,当对话框关闭时输出打印后



然而,当使用Chrome菜单或⌘P打印时,它会记录打印之前在打印对话框打开后打印到控制台。



这是我使用的代码,在其他浏览器中运行良好。 / p>

 函数beforePrint(){
console.log('Before Print');
}

函数afterPrint(){
console.log('After Print');
}

if(window.matchMedia){
var mediaQueryList = window.matchMedia('print');
mediaQueryList.addListener(function(mql){
(mql.matches)?beforePrint():afterPrint();
});
}
else {
//基本上是< IE11
window.addEventListener('beforeprint',beforePrint,false);
window.addEventListener('afterprint',afterPrint,false);
}

再一次清晰起来,好像这是控制台:



这是我期望的输出:

 > window.print()
之前打印
//打印对话框现在打开,我按取消关闭对话框
打印

这是我使用⌘P或菜单开始打印时获得的输出:

 之前打印
打印
之后//打印对话框现在打开,但发生在错误的地方

这是Chrome中的一个错误,还是有另一种方法可以正确捕获事件?

仅供参考,这里是我可以使用支持表来进行 matchMedia

解决方案

看起来这可能只是Chrome的一个漏洞。 https://bugs.chromium.org/p/chromium/issues/detail ?id = 422883


I am currently using the most up-to-date version of Chrome (43.0.2357.130), and am experiencing a difference in printing functionality when calling print from window.print() vs using ⌘P.

When printing using window.print() it outputs to the console correctly. It outputs Before Print when the print dialog is opened, and After Print when the dialog is closed.

However, when using the Chrome menu, or ⌘P to print, it logs both Before Print, and After Print to the console when the print dialog opens.

Here is the code I am using, which 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 this was the console:

This is the output I expect:

> window.print()
  Before Print
  // Print Dialog is now open, I press cancel to close dialog
  After Print

This is the output I get when I use ⌘P or the menu to start print:

  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 capture the event correctly?

Just for reference, here is the Can I Use support table for matchMedia

解决方案

It looks like it is probably just a bug with Chrome. https://bugs.chromium.org/p/chromium/issues/detail?id=422883

这篇关于在Chrome中发现浏览器打印事件有两种不同的方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆