The HTML onafterprint event attribute is triggered when a page has started printing or if the print dialog box has been closed in the HTML document.
Syntax
Following is the syntax −
<tagname onafterprint=”script”></tagname>
Let us see an example of HTML onafterprint event Attribute−
Example
<!DOCTYPE html> <html> <head> <style> body { color: #000; height: 100vh; background-color: #FBAB7E; background-image: linear-gradient(62deg, #FBAB7E 0%, #F7CE68 100%); text-align: center; padding: 20px; } p { font-size: 1.1rem; } </style> </head> <body onafterprint="get()"> <h1>HTML onafterprint Event Attribute Demo</h1> <p>I'm a paragraph HTML element with some dummy text.</p> <p style="color:#db133a;">Now try to print this document.</p> <div class="show"></div> <script> function get() { document.body.style.background = "lightblue"; } </script> </body> </html>
Output
Now try to print the document by using ctrl + p and observe how onafterprint event attribute works.