HTML Basic
HTML Reference
- HTML Tags
- HTML Deprecated Tags
- HTML Events
- HTML Attributes
- accept
- accept-charset
- accesskey
- action
- align
- alt
- as
- async
- autocomplete
- autofocus
- autoplay
- bgcolor
- border
- charset
- checked
- cite
- class
- color
- cols
- colspan
- content
- contenteditable
- controls
- coords
- data
- data-*
- datetime
- default
- defer
- dir
- dirname
- disabled
- download
- draggable
- enctype
- enterkeyhint
- for
- form
- formaction
- headers
- height
- hidden
- high
- href
- hreflang
- http-equiv
- id
- inert
- inputmode
- ismap
- kind
- label
- lang
- list
- loop
- low
- max
- maxlength
- media
- method
- min
- multiple
- muted
- name
- novalidate
- onabort
- onafterprint
- onbeforeprint
- onbeforeunload
- onblur
- oncanplay
- oncanplaythrough
- onchange
- onclick
- oncontextmenu
- oncopy
- oncuechange
- oncut
- ondblclick
- ondrag
- ondragend
- ondragenter
- ondragleave
- ondragover
- ondragstart
- ondrop
- ondurationchange
- onemptied
- onended
- onerror
- onfocus
- onhashchange
- oninput
- oninvalid
- onkeydown
- onkeypress
- onkeyup
- onload
- onloadeddata
- onloadedmetadata
- onloadstart
- onmousedown
- onmousemove
- onmouseout
- onmouseover
- onmouseup
- onmousewheel
- onoffline
- ononline
- onpagehide
- onpageshow
- onpaste
- onpause
- onplay
- onplaying
- onpopstate
- onprogress
- onratechange
- onreset
- onresize
- onscroll
- onsearch
- onseeked
- onseeking
- onselect
- onstalled
- onstorage
- onsubmit
- onsuspend
- ontimeupdate
- ontoggle
- onunload
- onvolumechange
- onwaiting
- onwheel
- open
- optimum
- pattern
- placeholder
- popover
- popovertarget
- popovertargetaction
- poster
- preload
- readonly
- rel
- required
- reversed
- rows
- rowspan
- sandbox
- scope
- selected
- shape
- size
- sizes
- span
- spellcheck
- src
- srcdoc
- srclang
- srcset
- start
- step
- style
- tabindex
- target
- title
- translate
- type
- usemap
- value
- width
- wrap
- HTML Global Attributes
- HTML Status Code
- HTML Language Code
- HTML Country Code
- HTML Charset
- MIME Types
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:
<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:
<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:
Author
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
If you have any doubts regarding this article (HTML onafterprint Attribute), please comment here. I will help you immediately.