W3cubDocs

/Web APIs

Window: beforeprint event

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

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
beforeprint_event 63 12 6 9 50 13 63 63 6 46 13 8.0

See also

© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeprint_event