Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / HTML

onafterprint — handling the post-print event

The onafterprint attribute on body sets a handler for the Window object's afterprint event. It can restore the interface after the printing procedure, but it does not confirm that the document was printed.

Version: WHATWG HTML Living Standard; index snapshot date recorded separately

Syntax

<body onafterprint="JavaScript statements">

onafterprint is an event handler content attribute on body. Its value contains JavaScript statements, not an event name or a Boolean value. Although written on body, it handles the Window object's afterprint event, not an event targeting the element itself.

The printing procedure provides beforeprint before offering printing and afterprint after the relevant steps of that procedure. The handler can restore interface changes made before printing. Do not treat it as confirmation of success: the user may decline printing, and the event does not confirm physical output.

The attribute corresponds to the window.onafterprint property; these are not two independent handler slots. An alternative is window.addEventListener('afterprint', handler). The example deliberately dispatches a synthetic event to check the attribute's connection to Window deterministically, without opening print UI. It requires JavaScript to be enabled and inline handlers to be permitted; a CSP policy can block them.

Examples

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>onafterprint test</title>
</head>
<body onafterprint="document.getElementById('status').textContent = 'afterprint received';">
  <p id="status">Waiting</p>
  <script>
    window.dispatchEvent(new Event('afterprint'));
  </script>
</body>
</html>

Result

afterprint received

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources