Skip to content
Related Articles

Related Articles

HTML | onbeforeprint Event Attribute

Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 16 Aug, 2022
Improve Article
Save Article

The onbeforeprint attribute works when a page is about to be printed. The alert message display before the print dialogue box appears. The onbeforeprint attribute is used with the onafterprint attribute.
Supported Tags 

Syntax: 
 

<element onbeforeprint = "script">

Attribute Value: This attribute contains single attribute value script and it works when document is about to print. This attribute is supported by <body> tag.
Example: 
 

html




<!DOCTYPE html>
<html>
    <head>
        <title>onbeforeprint attribute</title>
        <style>
            body {
                text-align:center;
            }
            h1 {
                color:green;
            }
        </style>
    </head>
    <body onbeforeprint="myFunction()">
        <h1>GeeksforGeeks</h1>
        <h2>onbeforeprint attribute</h2>
        <!-- The script run when page will ready to print -->
        <script>
            function myFunction() {
                alert("This document is ready to be printed");
            }
        </script>
    </body>
</html>

Output: 
 

Supported Browsers: The browser supported by onbeforeprint attribute are listed below: 
 

  • Chrome 63
  • Edge 12
  • Internet Explorer 9
  • Firefox 6
  • Safari 13
  • Opera 50

 

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!