👀 Live Preview
Use Print on this page — when the dialog closes, the status updates:
Open print dialog, then close it to trigger afterprint.

The onafterprint attribute is an inline event handler that runs JavaScript when the afterprint event fires. That event occurs on the window when the browser’s print dialog closes or after printing has started. Use it to restore screen UI that you changed for printing—for example removing a temporary class added in onbeforeprint. It does not guarantee the user actually printed; canceling the dialog still triggers afterprint in modern browsers.
Inline JS.
Window event.
Not print proof.
Prepare / restore.
Preferred way.
Property API.
onafterprint AttributeThe primary purpose of onafterprint is to run cleanup logic after the user finishes interacting with the print UI. A common pattern hides navigation during print preview in beforeprint and shows it again in afterprint. You might refresh analytics, revert DOM changes, or re-enable buttons that were disabled for the print flow.
The old reference said the document was “successfully printed.” That is inaccurate—afterprint means the print dialog closed, not that output reached a printer. Treat it as “print flow ended” rather than “print succeeded.”
Production code should use window.addEventListener("afterprint", handler). Inline onafterprint on <body> works for learning but separates concerns less cleanly.
Set onafterprint on body or assign window.onafterprint:
<script>
function handleAfterPrint() {
document.body.classList.remove("printing");
console.log("Print dialog closed.");
}
</script>
<body onafterprint="handleAfterPrint()">
<button type="button" onclick="window.print()">Print</button>
</body>afterprint event fires.window; body.onafterprint maps to the same event.onbeforeprint / beforeprint for prepare-and-restore flows.window.onafterprint = function() { … }.window.addEventListener("afterprint", handler).window.print() or the user choosing Print from the browser menu.The onafterprint attribute accepts a string of JavaScript code:
onafterprint="handleAfterPrint()" — Call a named function.onafterprint="console.log('done')" — Inline statement.window.onafterprint = () => { … } assigns the handler.window.addEventListener("afterprint", () => {
document.body.classList.remove("print-mode");
});
window.addEventListener("beforeprint", () => {
document.body.classList.add("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 |
| Restore UI | In afterprint handler | Remove print-only classes |
| Print CSS | @media print { … } | Often enough alone |
| Target | Supported? | Notes |
|---|---|---|
window | Yes | Primary event target |
<body> | Yes | Inline onafterprint 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 onafterprint, addEventListener on window, and pairing with beforeprint.
Use Print on this page — when the dialog closes, the status updates:
Open print dialog, then close it to trigger afterprint.
Call a function when the print dialog closes:
<body onafterprint="handleAfterPrint()">
<button onclick="window.print()">Print</button>
</body>
<script>
function handleAfterPrint() {
console.log("Print dialog closed.");
}
</script>The browser fires afterprint on window when the print UI closes. The inline attribute on body wires up the same handler.
Attach the handler with addEventListener:
<script>
window.addEventListener("afterprint", () => {
console.log("Dynamic logic after print dialog.");
});
// Or property form:
window.onafterprint = function () { /* … */ };
</script>Register the listener once at page load. Every time the user opens and closes the print dialog, afterprint runs your cleanup code.
Restore screen styles after printing:
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.
User prints or cancels.
afterprint event fires.
Cleanup handler runs.
The afterprint event and onafterprint 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.
onafterprint supportAll major browsers fire the underlying event and honor the onafterprint handler attribute.
Bottom line: Widely supported in modern browsers. Test print and cancel flows in your target browsers.
onafterprint.The onafterprint attribute runs JavaScript when the print dialog closes and the afterprint event fires on window. Use it to restore UI changes made for printing.
Pair it with onbeforeprint, prefer addEventListener("afterprint", …), and remember the event means “print flow ended,” not “print succeeded.”
onafterprintBookmark these before wiring your next event handler.
afterprint fires.
EventNot img/form.
ScopeAfter beforeprint.
PatternPreferred.
ModernCancel too.
Noteafterprint event fires — when the print dialog closes or printing starts.window via addEventListener("afterprint", …). Inline onafterprint 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 afterprint when you need script-driven cleanup.window.print() or use the browser Print command, then close the dialog to fire afterprint.Practice inline onafterprint, addEventListener, and beforeprint pairing in the Try It editor.
5 people found this page helpful