onafterprint
onafterprint event
The onafterprint
event fires after the print dialog closes, regardless of whether the user completes or cancels the print. This event can be used to execute scripts once the printing process is finished. It can be added to the <body>
or specific
elements to track this event.
Syntax
In HTML
<element onafterprint="myScript">
In JS
object.onafterprint = function(){myScript};
addEventListener()
function is used. In JavaScript, the
object.addEventListener("afterprint", myScript);
Example
<!DOCTYPE html>
<html>
<h1>HTML DOM Events</h1>
<h2>The afterprint Event</h2>
<body onafterprint="myFunction()">
<h3>Print this document!</h3>
<p><b>Tip</b>: The keyboard shortcut Ctrl+P prints a page.</p>
<script>
function myFunction() {
alert("This document is now being printed");
}
</script>
</body>
</html>
Values
<script>
- Specifies the script to execute when the event is triggered.
onafterprint
event is often used alongside onbeforeprint
to handle print-related tasks effectively.Conclusion
The onafterprint
event enables scripts to run after the print dialog closes, making it useful for post-print tasks. While widely supported, it is not available in Internet Explorer or Edge.
domcontentLoaded
This guide explores four important JavaScript events—DOMContentLoaded, load, beforeunload, and unload—that control webpage behavior at various points during user interaction and the page lifecycle.
onbeforeprint
The onbeforeunload event activates a function before the page is unloaded, giving you the opportunity to warn users about unsaved changes or show a confirmation dialog when they try to navigate away or reload the page.