Open In App

HTML onafterprint Event Attribute

Improve
Improve
Like Article
Like
Save
Share
Report

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:

  • This attribute contains a single value which works when an event attribute is called. This attribute is associated with <body> tag.

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: 

qa

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:

kio

Supported Browsers:

  • Google Chrome 1 and above
  • Edge 12 and above
  • Firefox 1 and above
  • Opera 15 and above
  • Safari 4 and above


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