WindowEventHandlers.onbeforeprint

WindowEventHandlers mixin 的 onbeforeprint 属性是用于处理当前 windowbeforeprint 事件的事件处理器(EventHandler)。These events are raised before the print dialog window is opened.

The beforeprint and afterprint events allow pages to change their content before printing starts (perhaps to remove a banner, for example) and then revert those changes after printing has completed. In general, you should prefer the use of an @media print CSS at-rule, but it may be necessary to use these events in some cases.

语法

window.addEventListener("beforeprint", function(event) { ... });
window.onbeforeprint = function(event) { ... };

Polyfill

一些基于 Webkit 内核的浏览器,比如 Safari,没有实现这些事件,但是你可以通过 window.matchMedia('print').得到与 beforeprint 事件等价的效果。

var mediaQueryList = window.matchMedia('print');
mediaQueryList.addListener(function(mql) {
  if(mql.matches) {
    console.log('webkit equivalent of onbeforeprint');
  }
});

规范

规范 状态 备注
HTML Living Standard
onbeforeprint
Living Standard

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
onbeforeprintChrome Full support 63Edge Full support YesFirefox Full support 6IE Full support YesOpera Full support 50Safari Full support 13WebView Android Full support 63Chrome Android Full support 63Firefox Android ? Opera Android Full support 46Safari iOS Full support 13Samsung Internet Android Full support 8.0

Legend

Full support  
Full support
Compatibility unknown  
Compatibility unknown

相关链接