W3cubDocs

/DOM

WindowEventHandlers.onbeforeprint

The property onbeforeprint sets and returns the onbeforeprint event handler code for the current window.

Syntax

window.onbeforeprint = event handling code

Notes

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 content authors should prefer the use of an @media print CSS at-rule, but it may be necessary to use these events in some cases.

The beforeprint event is raised before the print dialog window is opened.

Safari does not implement these events, but you can create an equivalent result to the beforeprint event with window.matchMedia('print').

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

Specification

Browser compatibilityUpdate compatibility data on GitHub

Desktop
Chrome Edge Firefox Internet Explorer Opera Safari
Basic support 63 Yes 6 Yes 50 No
Mobile
Android webview Chrome for Android Edge Mobile Firefox for Android Opera for Android iOS Safari Samsung Internet
Basic support 63 63 Yes ? 50 ? ?

See also

© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeprint