HTML onafterprint Attribute

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

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.

HTML Syntax

syntax.html
<body onafterprint=" script">

script runs when afterprint fires. Preferred: window.addEventListener("afterprint", handler).

Attribute Values

How you listen for afterprint:

Value / formDescriptionTry it
JavaScript on bodyInline handler, e.g. restoreScreen()Try it
addEventListener("afterprint")Preferred modern listener on windowTry it
With beforeprintPrepare for print, then restore afterwardTry it

Live Preview

Open the system print dialog, or simulate afterprint without printing. Status updates when the event fires.

action

Print opens the browser dialog. Simulate dispatches afterprint for study when you cannot print.

Demo

Waiting for afterprint…

HTML Examples

Short study snippets. Use Try It Yourself for a live editor. Prefer listeners + CSS print rules in real apps.

Example 1 — Inline onafterprint on body

Recognize the attribute form. Open print, then close the dialog to see the message.

HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>onafterprint demo</title>
</head>
<body onafterprint="document.getElementById('status').textContent = 'afterprint fired.'">
  <p id="status">Open print, then close the dialog.</p>
  <button type="button" onclick="window.print()">Print…</button>
</body>
</html>
Try It Yourself

How It Works

  1. onafterprint on body registers a window-level handler.
  2. window.print() opens the browser print UI.
  3. afterprint runs when printing starts or preview closes.
  4. Status text confirms the handler ran.

Example 2 — Preferred addEventListener

Same event, cleaner separation of HTML and script.

HTML
<p id="status">Waiting for afterprint…</p>
<button type="button" id="printBtn">Print…</button>

<script>
  var status = document.getElementById("status");

  window.addEventListener("afterprint", function () {
    status.textContent = "afterprint fired.";
  });

  document.getElementById("printBtn").addEventListener("click", function () {
    window.print();
  });
</script>
Try It Yourself

How It Works

  1. No attribute — the listener is attached on window.
  2. Same afterprint signal as the inline form.
  3. Easier to maintain — no markup/script mix.
  4. IDL option: window.onafterprint = handler also works (replaces prior property handlers).

Example 3 — beforeprint + afterprint

Hide a banner for print, then show it again afterward.

HTML
<div id="banner">Screen-only promo</div>
<p id="status">Idle</p>
<button type="button" id="printBtn">Print…</button>

<script>
  var banner = document.getElementById("banner");
  var status = document.getElementById("status");

  window.addEventListener("beforeprint", function () {
    banner.hidden = true;
    status.textContent = "beforeprint — banner hidden.";
  });

  window.addEventListener("afterprint", function () {
    banner.hidden = false;
    status.textContent = "afterprint — banner restored.";
  });

  document.getElementById("printBtn").addEventListener("click", function () {
    window.print();
  });
</script>
Try It Yourself

How It Works

  1. beforeprint prepares the page (hide promo).
  2. afterprint restores the screen layout.
  3. CSS first: for simple hide/show, @media print is often enough.
  4. 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.

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 Chrome 63+
Full support
Mozilla Firefox 6+
Full support
Apple Safari 13+
Full support
Microsoft Edge 12+
Full support
Opera 50+
Full support
Samsung Internet Modern
Full support
Internet Explorer Legacy*
Supported*
afterprint Widely supported

Bottom line: *Safe on modern browsers. Prefer @media print for styling; use afterprint to restore JS-driven print tweaks.

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.

Next: onbeforeprint

Run script just before the print dialog opens.

onbeforeprint tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful