The HTML onbeforeprint event attribute is triggered when a page is about to be printed or before the print dialog box appears in the HTML document.
Syntax
Following is the syntax −
<tagname onbeforeprint=”script”></tagname>
Let us see an example of HTML onbeforeprint 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 onbeforeprint="get()"> <h1>HTML onbeforeprint 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 onbeforeprint event attribute works.