A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/html/html-dom-onbeforeprint-event/ below:

HTML DOM onbeforeprint Event - GeeksforGeeks

HTML DOM onbeforeprint Event

Last Updated : 12 Jul, 2025

The onbeforeprint event occurs when a user gives a command to print a page. A dialog box will display just before printing the page. The onbeforeprint event is the opposite of the onafterprint event.

Supported Tags

Syntax: 

<element onbeforeprint="myScript">
object.onbeforeprint = function(){myScript};
object.addEventListener("beforeprint", myScript);

The below examples illustrate the onbeforeprint Event in HTML DOM:

Example: Using HTML 

HTML
<!DOCTYPE html>
<html>

<body onbeforeprint="myFunction()">
    <center>
        <h1 style="color:green">
            GeeksforGeeks
        </h1>
        <p>
              Try to print this page you will see a alert
          </p>

        <script>
            function myFunction() {
                alert("You are going to print this page");
            }
        </script>
    </center>
</body>
</html>

Output: 

 

Example: Using javascript 

HTML
<!DOCTYPE html>
<html>

<body>
    <center>
        <h1 style="color:green">
            GeeksforGeeks
        </h1>
        <p>
              Try to print this page you will see a alert
          </p>

        <script>
            document.getElementsByTagName("BODY")[0]
                .onbeforeprint = function () {
                myFunction()
            };

            function myFunction() {
                alert("You are going to print this page");
            }
        </script>
    </center>
</body>

</html>

Output: 

 

Example: In JavaScript, using the addEventListener() method: 

HTML
<!DOCTYPE html>
<html>

<body>
    <center>
        <h1 style="color:green">
            GeeksforGeeks
        </h1>
        <p>
              Try to print this page you will see a alert
          </p>

        <script>
            window.addEventListener(
               "beforeprint", myFunction);
            function myFunction() {
                alert("You are going to print this page");
            }
        </script>
    </center>
</body>

</html>

Output: 

 

Supported Browsers: The browser supported by HTML DOM onbeforeprint events are listed below: 



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4