References

Beginner-friendly references for web development, with live, editable examples.

The HTML onbeforeprint event

Event All modern browsers
In short

The HTML onbeforeprint attribute runs JavaScript right before the page is printed or shown in print preview. For example, <body onbeforeprint="addPrintDate()"> can stamp the date onto the printed copy. Pair it with onafterprint to undo the change afterward.

Overview

The HTML onbeforeprint attribute handles the beforeprint event of the window. That event fires when the document is about to be printed or previewed. As an HTML attribute it belongs on the <body> element, and in a script it is window.onbeforeprint.

Two actions start printing. A call to window.print() fires the event, and the HTML standard says browsers should do the same whenever the user asks for a printed copy or a PDF.

After the load event has finished, the handler runs before print() returns. A print() call made earlier, even from an onload handler, returns at once, and printing waits until then.

Use onbeforeprint to annotate the printed copy, such as adding the time it was printed, and revert that in onafterprint.

The event fires in child frames too, so an iframe receives beforeprint when its parent prints. A frame sandboxed without allow-modals can't start printing itself, though. Its own print() call is ignored, no event fires for it, and Chrome logs Ignored call to 'print()'.

Syntax

<body onbeforeprint="addPrintDate()" onafterprint="removePrintDate()">

Example

Live example
<p>Order 1024, three items, ready for pickup.</p>
<p id="stamp"></p>
<button type="button" onclick="window.print()">Print this order</button>
<p>The print preview shows a "Printed" line with the current date and time.</p>
<script>
window.onbeforeprint = function () {
  document.getElementById('stamp').textContent = 'Printed ' + new Date().toLocaleString();
};
window.onafterprint = function () {
  document.getElementById('stamp').textContent = '';
};
</script>

Best practices

  • Reach for an @media print rule first, and add onbeforeprint only for changes that need script.
  • Undo every onbeforeprint change in onafterprint, so the page on screen looks the same once the print dialog closes.
  • Make the changes synchronously inside the handler. The browser prints the page as it stands right after the event, so a later async update can miss the printout.
  • Test with the browser's own Print command as well as window.print(). Both are meant to fire the same events, so the handler must work even when nobody clicks the page's print button.

Frequently asked questions

What does onbeforeprint do?
The onbeforeprint attribute runs JavaScript right before the page is printed or previewed for printing. It suits last-moment changes, such as stamping the print date.
How is onbeforeprint different from onafterprint?
The beforeprint event fires before printing starts. The afterprint event fires once printing has started or the print preview has closed, so use onafterprint to undo whatever onbeforeprint changed.
Does onbeforeprint fire when the user presses Ctrl+P?
Yes, the HTML standard asks browsers to fire beforeprint for any print request from the user. Pressing Ctrl+P, or Cmd+P on a Mac, counts, and so does choosing Print from the browser's menu, whether the result goes to paper or a PDF.
Should I use onbeforeprint or @media print?
Use @media print for layout and visibility changes, and keep onbeforeprint for what CSS can't do. Inserting the current date and time is one such change.
Can onbeforeprint cancel printing?
No, the beforeprint event can't be canceled, so a handler can change the page but not stop the print.
Why doesn't onbeforeprint fire when my iframe calls print()?
An iframe sandboxed without allow-modals can't print, so its own print() call is ignored and doesn't run onbeforeprint. Printing the parent page still fires beforeprint inside that frame.