👀 Live Preview
Use Print on this page — when the dialog opens, the status updates:
Open the print dialog to trigger beforeprint.

The onbeforeprint attribute is an inline event handler that runs JavaScript when the beforeprint event fires. That event occurs on the window when the browser’s print dialog opens or print preview starts. Use it to prepare the page for printing—for example hiding navigation, adding a temporary class, or updating content before the print layout is rendered. Pair it with onafterprint to restore the screen UI when the dialog closes. It does not mean the document was sent to a printer; it runs at the start of the print flow.
Inline JS.
Window event.
Prepare layout.
Prepare / restore.
Preferred way.
Property API.
onbeforeprint AttributeThe primary purpose of onbeforeprint is to run preparation logic before the user sees the print preview or print dialog. A common pattern hides navigation and sidebars in beforeprint and shows them again in afterprint. You might inject print-only headers, expand collapsed sections, or swap chart data for a static snapshot.
The old reference said the document was “sent to the printer.” That is inaccurate—beforeprint means the print UI is opening, not that output reached a printer. Treat it as “print flow starting” rather than “print succeeded.”
Production code should use window.addEventListener("beforeprint", handler). Inline onbeforeprint on <body> works for learning but separates concerns less cleanly.
Set onbeforeprint on body or assign window.onbeforeprint:
<script>
function handleBeforePrint() {
document.body.classList.add("printing");
console.log("Print dialog opening.");
}
</script>
<body onbeforeprint="handleBeforePrint()">
<button type="button" onclick="window.print()">Print</button>
</body>beforeprint event fires.window; body.onbeforeprint maps to the same event.onafterprint / afterprint for prepare-and-restore flows.window.onbeforeprint = function() { … }.window.addEventListener("beforeprint", handler).window.print() or the user choosing Print from the browser menu.The onbeforeprint attribute accepts a string of JavaScript code:
onbeforeprint="handleBeforePrint()" — Call a named function.onbeforeprint="console.log('prepare')" — Inline statement.window.onbeforeprint = () => { … } assigns the handler.window.addEventListener("beforeprint", () => {
document.body.classList.add("print-mode");
});
window.addEventListener("afterprint", () => {
document.body.classList.remove("print-mode");
});| Event | When it fires | Handler |
|---|---|---|
beforeprint | Print dialog opens | onbeforeprint |
afterprint | Print dialog closes | onafterprint |
| Open print UI | window.print() | User or script |
| Prepare UI | In beforeprint handler | Add print-only classes |
| Print CSS | @media print { … } | Often enough alone |
| Target | Supported? | Notes |
|---|---|---|
window | Yes | Primary event target |
<body> | Yes | Inline onbeforeprint attribute |
<img>, <form> | No | Not print events |
onbeforeprint vs onafterprint| Handler | Timing | Typical use |
|---|---|---|
onbeforeprint | Before / when dialog opens | Add print styles, hide chrome |
onafterprint | When dialog closes | Restore screen layout |
Inline onbeforeprint, addEventListener on window, and pairing with afterprint.
Use Print on this page — when the dialog opens, the status updates:
Open the print dialog to trigger beforeprint.
Call a function when the print dialog opens:
<body onbeforeprint="handleBeforePrint()">
<button onclick="window.print()">Print</button>
</body>
<script>
function handleBeforePrint() {
console.log("Print dialog opening.");
}
</script>The browser fires beforeprint on window when the print UI opens. The inline attribute on body wires up the same handler.
Attach the handler with addEventListener:
<script>
window.addEventListener("beforeprint", () => {
console.log("Dynamic logic before print dialog.");
});
// Or property form:
window.onbeforeprint = function () { /* … */ };
</script>Register the listener once at page load. Every time the user opens the print dialog, beforeprint runs your preparation code before the print layout is rendered.
Prepare print styles before printing, then restore on close:
window.addEventListener("beforeprint", () => {
document.body.classList.add("print-mode");
});
window.addEventListener("afterprint", () => {
document.body.classList.remove("print-mode");
});beforeprint prepares the page; afterprint undoes temporary changes so the screen view returns to normal.
@media print so content remains readable on paper without relying only on JS.window.print() or browser menu.
Handler runs first.
User prints or cancels.
Print layout ready.
The beforeprint event and onbeforeprint handler are supported in Chrome 63+, Firefox 6+, Safari 13+, and Edge 79+. Older browsers may not fire these events; rely on @media print CSS as a baseline.
onbeforeprint supportAll major browsers fire the underlying event and honor the onbeforeprint handler attribute.
Bottom line: Widely supported in modern browsers. Test print preparation and cancel flows in your target browsers.
onbeforeprint.The onbeforeprint attribute runs JavaScript when the print dialog opens and the beforeprint event fires on window. Use it to prepare the page for printing before the layout is rendered.
Pair it with onafterprint, prefer addEventListener("beforeprint", …), and remember the event means “print flow starting,” not “print succeeded.”
onbeforeprintBookmark these before wiring your next event handler.
beforeprint fires.
EventNot img/form.
ScopeBefore afterprint.
PatternPreferred.
ModernOpens early.
Notebeforeprint event fires — when the print dialog opens or print preview starts.window via addEventListener("beforeprint", …). Inline onbeforeprint on body also works.onbeforeprint runs when the dialog opens (prepare). onafterprint runs when it closes (restore).@media print { … } handles most print styling without JavaScript. Use beforeprint when you need script-driven preparation.window.print() or use the browser Print command. beforeprint fires as soon as the print UI opens.Practice inline onbeforeprint, addEventListener, and afterprint pairing in the Try It editor.
5 people found this page helpful