Horje

Tips (Total 1)


# Tips-1) What is HTML onafterprint Event Attribute

Definition and Usage

The onafterprint attribute fires when a page has started printing, or if the print dialogue box has been closed.

Tip: The onafterprint attribute is often used together with the onbeforeprint attribute.


Browser Support

Note: In IE/Edge, the onafterprint attribute occurs before the print dialogue box, instead of after.


Syntax

<element onafterprint="script">

Attribute Values

Value Description
script The script to be run on onafterprint

Technical Details

Supported HTML tags: <body>

How to create HTML onafterprint Event Attribute

Execute a JavaScript when a page has started printing, or if the print dialogue box has been closed.
index.html
Example: HTML
 <body onafterprint="myFunction()"> 

Output should be:

How to create HTML onafterprint Event Attribute

How to Execute a JavaScript when a page has started printing, or if the print dialogue box has been closed

Try to print this document Tip: Keyboard shortcuts, such as Ctrl+P sets the page to print. Note: The onafterprint event is not supported in Safari and Opera. Note: In IE, the onafterprint event occurs before the print dialogue box, instead of after.
index.html
Example: HTML
<!DOCTYPE html>
<html>
<body onafterprint="myFunction()">

<h1>Try to print this document</h1>
<p><b>Tip:</b> Keyboard shortcuts, such as Ctrl+P sets the page to print.</p>
<p><b>Note:</b> The onafterprint event is not supported in Safari and Opera.</p>
<p><b>Note:</b> In IE, the onafterprint event occurs before the print dialogue box, instead of after.</p>

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

</body>
</html>

Output should be:

How to Execute a JavaScript when a page has started printing, or if the print dialogue box has been closed