Window: beforeprint event

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2019.

The beforeprint event is fired when the associated document is about to be printed or previewed for printing.

The afterprint and beforeprint 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 a @media print CSS at-rule, but it may be necessary to use these events in some cases.

Syntax

Use the event name in methods like addEventListener(), or set an event handler property.

js
addEventListener("beforeprint", (event) => {});
onbeforeprint = (event) => {};

Event type

A generic Event.

Examples

Using addEventListener():

js
window.addEventListener("beforeprint", (event) => {
  console.log("Before print");
});

Using the onbeforeprint event handler property:

js
window.onbeforeprint = (event) => {
  console.log("Before print");
};

Specifications

Specification
HTML
# event-beforeprint
HTML
# handler-window-onbeforeprint

Browser compatibility

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
beforeprint event

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

See also