Open In App

HTML onbeforeprint Event Attribute

The onbeforeprint event fires when a page is about to be printed before the print dialog box appears. This event can be used to perform actions before printing, such as hiding unnecessary elements or preparing the page for printing.

Syntax: 

<element onbeforeprint = "script">

Supported Tags 

Attribute Value:

This attribute contains the script, this script is to be run on onbeforeprint



Example 1: In this example, we will see the implementation of the onbeforeprint Event attribute.




<!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: 



Output

Example 2: In this example, we will see the implementation of the onbeforeprint Event attribute.




<!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() {
            console.log("This document is ready to be printed")
        }
    </script>
</body>
 
</html>

Output:

Output

Supported Browsers:


Article Tags :