Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

HTML Basic

HTML Reference

HTML onafterprint Attribute

Posted in HTML Tutorial
Updated on Jan 26, 2024
By Mari Selvan
👁️ 18 - Views
⏳ 4 mins
💬 1 Comment
HTML onafterprint Attribute

Photo Credit to CodeToFun

🙋 Introduction

The onafterprint attribute is an event handler in HTML that allows developers to execute a script or perform actions after a document has been successfully printed.

This attribute is part of the event-handling attributes in HTML and provides a way to trigger specific actions when the printing process is completed.

🎯 Purpose of onafterprint

The primary purpose of the onafterprint attribute is to enable developers to respond to the completion of a printing job.

This can be useful for scenarios where additional actions need to be taken after a document has been successfully printed, such as updating the UI or triggering other events.

💎 Values

The onafterprint attribute accepts a JavaScript function as its value.

This function is executed when the afterprint event occurs, signaling that the document has been successfully printed. Here's a basic example:

onafterprint.html
Copied
Copy To Clipboard
<script>
  function handleAfterPrint() {
    // Your custom logic after printing
    alert("Document has been printed successfully!");
  }
</script>

<body onafterprint="handleAfterPrint()">
  <!-- Your document content goes here -->
</body>

🧠 How it Works

In this example, the handleAfterPrint function is called when the afterprint event occurs, allowing you to define custom logic to be executed after the document is printed.

🔄 Dynamic Values with JavaScript

You can dynamically assign the onafterprint attribute using JavaScript, allowing for more flexibility in handling print events. Here's a brief example:

onafterprint.html
Copied
Copy To Clipboard
<script>
  // Dynamically set onafterprint for the body element
  document.body.onafterprint = function() {
    // Your dynamic logic after printing
    console.log("Dynamic logic executed after printing");
  };
</script>

🧠 How it Works

In this script, the onafterprint attribute for the body element is dynamically set to an anonymous function.

This can be beneficial when you want to change the behavior based on specific conditions or user interactions.

🏆 Best Practices

  • Use the onafterprint attribute when you need to perform specific actions or updates after the document has been successfully printed.
  • Ensure that the function assigned to onafterprint is well-tested to handle various scenarios that may occur after printing.
  • Be mindful of browser compatibility, as not all browsers may fully support the onafterprint attribute.

🎉 Conclusion

The onafterprint attribute provides a valuable mechanism for executing custom scripts or actions after a document has been printed.

By utilizing this attribute, you can enhance the user experience and responsiveness of your web applications to printing events.

👨‍💻 Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Mari Selvan
Mari Selvan
3 months ago

If you have any doubts regarding this article (HTML onafterprint Attribute), please comment here. I will help you immediately.

We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy