A RetroSearch Logo

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

Search Query:

Showing content from https://www.geeksforgeeks.org/html/html-onafterprint-event-attribute/ below:

HTML onafterprint Event Attribute - GeeksforGeeks

HTML onafterprint Event Attribute

Last Updated : 28 Jul, 2025

The HTML onafterprint Event Attribute works when a page has started printing, or if the print dialogue box has been closed. This attribute is used together with the onbeforeprint attribute. This attribute is typically associated with the window or document element and is part of the WindowEventHandlers interface.

Basically, it is fired after a document is successfully printed or previewed and is useful for executing actions specifically intended for post-printing scenarios.

Syntax: 
<element onafterprint = "script">
Supported Tags: Attribute Value:

Example 1: In this example, we will see the implementation of onafterprint Event Attribute.

html
<!DOCTYPE html>
<html>

<head>
    <title>title attribute</title>
    <style>
        body {
            text-align: center;
        }

        h1 {
            color: green;
        }
    </style>
</head>

<body onafterprint="myFunction()">
    <h1>
        GeeksforGeeks
    </h1>
    <h2>
        onafterprint attribute
    </h2>

    <!-- The script run when page will print -->

    <script>
        function myFunction() {
            alert("This document is now being printed")
        }
    </script>
</body>

</html>

Output: 

Example 2: In this example, we will see the implementation of onafterprint Event Attribute.

HTML
<!DOCTYPE html>
<html>

<head>
    <title>title attribute</title>
    <style>
        body {
            text-align: center;
        }

        h1 {
            color: green;
        }
    </style>
</head>

<body onafterprint="myFunction()">
    <h1>
        GeeksforGeeks
    </h1>
    <h2>
        onafterprint attribute
    </h2>

    <!-- The script run when page will print -->

    <script>
        function myFunction() {
            console.log("This document is now being printed")
        }
    </script>
</body>

</html>

Output:

Supported Browsers:

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