HTML onafterprint Event Attribute
Description
The HTML onafterprint event attribute fires when a page has started printing, or if the print dialog box has been closed. It is a new event attribute in HTML5, and is supported by all major browsers except Internet Explorer and Edge.
The onafterprint event can be used to perform tasks such as:
- Updating the UI to indicate that the print job is complete.
- Hiding or disabling elements that are not needed for printing.
- Performing cleanup operations, such as closing open files or releasing resources.
The onafterprint event is often used together with the onbeforeprint event attribute, which fires before the page starts printing. This allows developers to perform different tasks before and after the print job, such as hiding and showing different elements of the page.
Here is an example of how to use the onafterprint event attribute:
<body onafterprint="myAfterPrintFunction()">
...
</body>
The myAfterPrintFunction() function will be called when the page has started printing, or if the print dialog box has been closed.
Here is an example of a simple myAfterPrintFunction() function:
function myAfterPrintFunction() {
// Update the UI to indicate that the print job is complete.
document.getElementById("printStatus").innerHTML = "Print job complete!";
}
The onafterprint event can be used in a variety of ways to improve the user experience of printing web pages.
Syntax
<element onafterprint="script">
Values
- scriptThe name of the script to use when the event has been triggered.
Example
Browser Support
The following information will show you the current browser support for the HTML onafterprint event attribute. Hover over a browser icon to see the version that first introduced support for this HTML event attribute.
This event attribute is supported by all modern browsers.
Desktop
Tablets & Mobile
Last updated by CSSPortal on: 14th October 2023
