Document
Document Event
HTML document
event attributes are used to handle events related to the overall document, such as when the page loads, when it’s clicked, or when the document is ready for interaction. These events help developers manage the document's behavior and user interactions.
Common Document Events
onload
: Triggered when the document (or an element) has fully loaded.
<body onload="alert('Document loaded!')">
onunload
: Occurs when the document is about to be unloaded (e.g., when the user navigates away or closes the window).
<body onunload="alert('Document is being unloaded!')">
onclick
: Fired when the user clicks anywhere on the document.
<body onclick="alert('Document clicked!')">
onresize
: Activated when the document is resized (though often used with the window).
<body onresize="alert('Document resized!')">
ondblclick
: Triggered when the user double-clicks anywhere on the document.
<body ondblclick="alert('Document double-clicked!')">
onkeydown
: Occurs when a key is pressed down in the document.
<body onkeydown="alert('Key pressed!')">
onkeyup
: Triggered when a key is released in the document.
<body onkeyup="alert('Key released!')">
These event attributes are used to handle various user interactions with the document and manage behaviors accordingly. They can improve user experience and provide responsive web applications.
Conclusion
The document
event attributes in HTML offer powerful ways to manage user interactions and changes in the overall document's state. By listening to events like onload
, onclick
, and onresize
, developers can enhance the user experience and create more interactive, responsive web pages. These events allow for dynamic reactions to user actions, such as clicks, keyboard inputs, or page loads, providing greater control over how content is presented and how the page behaves based on user activity.
clipboard
HTML clipboard event attributes manage interactions for copying, cutting, and pasting content, allowing custom handling of clipboard actions.
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.