HTML 4 added the ability to let events trigger actions in a browser, like starting a JavaScript when a user clicks on an element.
We can write our event handlers in Javascript or vbscript and you can specify these event handlers as a value of event tag attribute. The HTML5 specification defines various event attributes as listed below:
There are following attributes which can be used to trigger any javascript or vbscript code given as value, when there is any event occurs for any HTM5 element.
We would cover element specific events while discussing those elements in detail in subsequent chapters.
Window Event Attributes
Events triggered for the window object (applies to the <body> tag):
Attribute | Value | Description |
onafterprint | script | Script to be run after the document is printed |
onbeforeprint | script | Script to be run before the document is printed |
onbeforeunload | script | Script to be run before the document is unloaded |
onerror | script | Script to be run when an error occur |
onhaschange | script | Script to be run when the document has changed |
onload | script | Fires after the page is finished loading |
onmessage | script | Script to be run when the message is triggered |
onoffline | script | Script to be run when the document goes offline |
ononline | script | Script to be run when the document comes online |
onpagehide | script | Script to be run when the window is hidden |
onpageshow | script | Script to be run when the window becomes visible |
onpopstate | script | Script to be run when the window's history changes |
onredo | script | Script to be run when the document performs a redo |
onresize | script | Fires when the browser window is resized |
onstorage | script | Script to be run when a Web Storage area is updated |
onundo | script | Script to be run when the document performs an undo |
onunload | script | Fires once a page has unloaded (or the browser window has been closed) |
Form Events
Events triggered by actions inside a HTML form (applies to almost all HTML5
elements, but is most used in form elements):
Attribute | Value | Description |
onblur | script | Fires the moment that the element loses focus |
onchange | script | Fires the moment when the value of the element is changed |
oncontextmenu | script | Script to be run when a context menu is triggered |
onfocus | script | Fires the moment when the element gets focus |
onformchange | script | Script to be run when a form changes |
onforminput | script | Script to be run when a form gets user input |
oninput | script | Script to be run when an element gets user input |
oninvalid | script | Script to be run when an element is invalid |
onreset | script | Fires when the Reset button in a form is clicked |
onselect | script | Fires after some text has been selected in an element |
onsubmit | script | Fires when a form is submitted |
Keyboard Events
Attribute | Value | Description |
onkeydown | script | Fires when a user is pressing a key |
onkeypress | script | Fires when a user presses a key |
onkeyup | script | Fires when a user releases a key |
Mouse Events
Events triggered by a mouse or similar user actions:
Attribute | Value | Description |
onclick | script | Fires on a mouse click on the element |
ondblclick | script | Fires on a mouse double-click on the element |
ondrag | script | Script to be run when an element is dragged |
ondragend | script | Script to be run at the end of a drag operation |
ondragenter | script | Script to be run when an element has been dragged to a valid drop target |
ondragleave | script | Script to be run when an element leaves a valid drop target |
ondragover | script | Script to be run when an element is being dragged over a valid drop target |
ondragstart | script | Script to be run at the start of a drag operation |
ondrop | script | Script to be run when dragged element is being dropped |
onmousedown | script | Fires when a mouse button is pressed down on an element |
onmousemove | script | Fires when the mouse pointer moves over an element |
onmouseout | script | Fires when the mouse pointer moves out of an element |
onmouseover | script | Fires when the mouse pointer moves over an element |
script | Fires when a mouse button is released over an element | |
onmousewheel | script | Script to be run when the mouse wheel is being rotated |
onscroll | script | Script to be run when an element's scrollbar is being scrolled |
Media Events
Events triggered by Medias like videos, images and audio (applies to all HTML5
elements, but is most common in media elements, like audio, embed, img, object,
and video):
Attribute | Value | Description |
onabort | script | Script to be run on abort |
oncanplay | script | Script to be run when a file is ready to start playing (when it has buffered enough to begin) |
oncanplaythrough | script | Script to be run when a file can be played all the way to the end without pausing for buffering |
ondurationchange | script | Script to be run when the length of the media changes |
onemptied | script | Script to be run when something bad happens and the file is suddenly unavailable (like unexpectedly disconnects) |
onended | script | Script to be run when the media has reach the end (a useful event for messages like "thanks for listening") |
onerror | script | Script to be run when an error occurs when the file is being loaded |
onloadeddata | script | Script to be run when media data is loaded |
onloadedmetadata | script | Script to be run when meta data (like dimensions and duration) are loaded |
onloadstart | script | Script to be run just as the file begins to load before anything is actually loaded |
onpause | script | Script to be run when the media is paused either by the user or programmatically |
onplay | script | Script to be run when the media is ready to start playing |
onplaying | script | Script to be run when the media actually has started playing |
onprogress | script | Script to be run when the browser is in the process of getting the media data |
onratechange | script | Script to be run each time the playback rate changes (like when a user switches to a slow motion or fast forward mode) |
onreadystatechange | script | Script to be run each time the ready state changes (the ready state tracks the state of the media data) |
onseeked | script | Script to be run when the seeking attribute is set to false indicating that seeking has ended |
onseeking | script | Script to be run when the seeking attribute is set to true indicating that seeking is active |
onstalled | script | Script to be run when the browser is unable to fetch the media data for whatever reason |
onsuspend | script | Script to be run when fetching the media data is stopped before it is completely loaded for whatever reason |
ontimeupdate | script | Script to be run when the playing position has changed (like when the user fast forwards to a different point in the media) |
onvolumechange | script | Script to be run each time the volume is changed which (includes setting the volume to "mute") |
onwaiting | script | Script to be run when the media has paused but is expected to resume (like when the media pauses to buffer more data) |
Leave Comment