Events

onafterprint

Printing has finished or the print preview has closed. Window event handler, exposed through the body element in HTML; register on window rather than on an arbitrary element. Event interface: Event. For the described native event: bubbles: no; cancelable: no. Restore temporary print-related UI; the event does not prove a physical page was printed. Example: Open print preview and close it; availability depends on browser/iframe policy. The event listener is registered with addEventListener(), which avoids inline script handlers and supports multiple listeners. Inline handlers require a CSP that permits them; do not insert untrusted code into handler attributes.

Formal Syntax & Structure

html syntaxhtml
<body onafterprint="handleEvent(event)">  window.addEventListener("afterprint", event => { /* handle event */ });

Parameters & Attributes

NameTypeDescription
onafterprintJavaScript handler bodyOptional HTML event-handler code; the example uses a registered listener instead. Not a Boolean attribute.
eventEventEvent object delivered to the addEventListener callback; event.type is "afterprint". Inspect target, currentTarget, bubbles and cancelable as appropriate.

Working Example

Example Snippethtml
<p>Window event monitor</p><button id="print" type="button">Print preview</button><p role="status" id="log">Waiting for afterprint</p><script>  const target = window;  const log = document.getElementById("log");  target.addEventListener("afterprint", event => {    log.textContent = String(event.type);  });  document.getElementById("print").onclick = () => window.print();</script>
HTML Reference