Open In App

HTML onbeforeprint Event Attribute

Improve
Improve
Like Article
Like
Save
Share
Report

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.

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: 

swe

Output

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

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


Output:

der

Output

Supported Browsers:

  • Google Chrome 63
  • Edge 12
  • Firefox 6
  • Opera 5
  • Safari 4


Last Updated : 26 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads