The onafterprint attribute handles the window afterprint event. It runs after the document has started printing or after the print preview is closed.
Put it on <body> (window handlers on body listen on window), or use window.addEventListener("afterprint", …). Pair with onbeforeprint when script must change content for print, then restore it. Prefer @media print for styling.
Baseline · Widely available.
Stable across modern browsers since about September 2019. Not deprecated.
Inline handlers discouraged.
MDN: prefer CSS for print layout, and addEventListener over inline on* attributes when script is required.
Syntax
HTML Syntax
syntax.html
<body onafterprint="script">
script runs when afterprint fires. Preferred: window.addEventListener("afterprint", handler).
CSS first: for simple hide/show, @media print is often enough.
Use JS when print needs temporary DOM changes CSS cannot cover.
Edge Cases & Pitfalls
Most issues come from using JS for styling that CSS print media can already handle.
css first
Prefer @media print
MDN recommends print CSS for layout. Use afterprint when you must mutate the DOM around printing.
window
Window-level event
Listen on window (or body for the HTML attribute). Putting it on a random div will not help.
preview
Preview cancel still counts
Closing print preview without printing still fires afterprint in typical browsers — restore UI either way.
style
Prefer listeners
Inline on* attributes mix concerns. Use addEventListener in production code.
pair
Always restore
If beforeprint hides or rewrites content, afterprint must put the screen view back.
Compatibility
Browser Support
onafterprint is an HTML / window event handler for the afterprint event. Prefer addEventListener and @media print in apps.
✓ Baseline · Widely available
onafterprint
Widely available since about September 2019. Chrome, Firefox, Safari, Edge, and Opera support afterprint on window.
95%Global support (approx.)
Google Chrome63+
Full support
Mozilla Firefox6+
Full support
Apple Safari13+
Full support
Microsoft Edge12+
Full support
Opera50+
Full support
Samsung InternetModern
Full support
Internet ExplorerLegacy*
Supported*
afterprintWidely supported
Bottom line: *Safe on modern browsers. Prefer @media print for styling; use afterprint to restore JS-driven print tweaks.
Remember
Key Takeaways
Role: run script after print starts or preview closes.
Pair with:beforeprint to prepare, then restore.
Prefer:@media print + addEventListener.
Target:window / body, not arbitrary elements.
One line:onafterprint restores the page after print/preview — use CSS print styles first.
Frequently Asked Questions
It is an event handler for the afterprint event. That event fires after the document has started printing or after the print preview is closed. Pages often use it to undo temporary changes made for printing.
It is a window-level handler. In HTML, put it on body (window event handlers on body listen on window), or use window.addEventListener("afterprint", …) / window.onafterprint.
Prefer CSS @media print for print styling. Use beforeprint/afterprint only when you must change the DOM with JavaScript around printing.
beforeprint runs as print/preview is about to start (prepare content). afterprint runs afterward (restore content). Use them as a pair when JS changes are required.
Prefer window.addEventListener("afterprint", handler). MDN discourages inline HTML event handler attributes.
Yes in typical browsers: afterprint also fires when the print preview is closed, not only when pages are sent to a printer.