HTML 4 allowed the events to trigger actions in a browser. Below are the global event attributes which can be added with HTML elements for defining action on events.
Window Event Attributes
– These attributes get triggered for the window object (it pertains to the <body> tag of HTML)
Window Event Attribute |
Description |
onafterprint | Defines the script to be run after printing the document |
onbeforeprint | Defines the script to be run before printing the document |
onbeforeunload | Defines the script to be run before unloading the document |
onerror | Defines the script to be run whenever an error is encountered |
onhaschange | Defines the script to be whenever the document has changed |
onload | Defines the script to be run after loading the document |
onmessage | Defines the script to be run whenever a message is triggered. |
onoffline | Defines the script to be run whenever the document goes offline. |
ononline | Defines the script to be run whenever the document goes online. |
onpagehide | Defines the script to be run whenever the document is hidden. |
onpageshow | Defines the script to be run whenever the document goes visible. |
onpopstate | Defines the script to be run whenever the window’s history changes. |
onredo | Defines the script to be run whenever the document performs a redo operation. |
onresize | Defines the script to be run whenever the window is resized. |
onstorage | Defines the script to be run whenever the webstorage area is updated. |
onundo | Defines the script to be run whenever the document does undo operation. |
onunload | Defines the script to be run whenever the document has been unloaded or when the browser is closed. |
Form Events Attributes
– These are the events that are triggered by performing actions within the HTML form. Mostly used in the FORM elements.
Form Event Attribute |
Description |
onblur | Defines the script to be run once the element loses focus |
onchange | Defines the script to be run once the element’s value changes |
oncontextmenu | Defines the script to be run once the context menu is triggered |
onfocus | Defines the script to be run once the element gets focus |
onformchange | Defines the script to be run once the form changes |
onforminput | Defines the script to be run once the form gets input from user |
oninput | Defines the script to be run once the element gets input from user |
oninvalid | Defines the script to be run once the elements gets invalid |
onreset | Defines the script to be run once reset button is clicked on the form (Not supported in HTML 5) |
onselect | Defines the script to be run some text has been selected within an element |
onsubmit | Defines the script to be run once the form gets submitted |
Keyboard Events Attributes
Keyboard Event Attribute |
Description |
onkeydown | Defines the script to be run once a key is being pressed |
onkeypress | Defines the script to be run once a key is pressed |
onkeyup | Defines the script to be run once a pressed key is released |
Mouse Events Attributes
– These are the events that are triggered by mouse or similar device
Mouse Event Attribute |
Description |
onclick | Defines the script to be run once mouse is clicked on the element |
ondbclick | Defines the script to be run once mouse is double clicked on the element |
ondrag | Defines the script to be run once an element is dragged |
ondragend | Defines the script to be run once the drag operation on an element is completed |
ondragenter | Defines the script to be run once an element is dragged to a valid drop place or target |
ondragleave | Defines the script to be run once an element leaves a valid drop place or target |
ondragover | Defines the script to be run once an element is being dragged to a valid drop place or target |
ondragstart | Defines the script to be run at the start of an element’s drag operation |
ondrop | Defines the script to be run when the dragged element is being dropped. |
onmousedown | Defines the script to be run once the mouse button is pressed down on an element |
onmousemove | Defines the script to be run once the mouse pointer is moving |
onmouseout | Defines the script to be run once the mouse pointer moves out form an element |
onmouseover | Defines the script to be run once the mouse pointer is moving over the element |
onmouseup | Defines the script to be run once the mouse button is released up on an element |
onmousewheel | Defines the script to be run once the mouse button is being scrolled or rotated |
onscroll | Defines the script to be run when the element’s scrollbar is being scrolled |
Media Event Attributes
– These are the events which are triggered by media elements like audio, images or videos, most commonly used with <audio>, <embed>, <img>, <object>, and <video> elements.
Media Event Attribute |
Description |
onabort | Defines the script to be run on abort |
oncanplay | Defines the script to be run once a file is ready to be played (i.e. when the file is buffered enough to play) |
oncanplaythrough | Defines the script to be run once a file can be played till the end without buffering. |
ondurationchange | Defines the script to be run when the length of the media file changes. |
onemptied | Defines the script to be run when an error is encountered in running the file (like the connection is disconnected) |
onended | Defines the script to be run once the media file has reached the end (can give a message like “Thanks for watching”) |
onerror | Defines the script to be run if an error occurs while the media is being loaded |
onloadeddata | Defines the script to be run when the media is loaded |
onloadedmetadata | Defines the script to be run when the meta data about the media is loaded |
onloadstart | Defines the script to be run just before the media starts to load |
onpause | Defines the script to be run when the running media is paused ( by user or by program) |
onplay | Defines the script to be run when the media is ready to be played. |
onplaying | Defines the script to be run when the media is being played. |
onprogress | Defines the script to be run when the browser is in the process to get the media data. |
onratechange | Defines the script to be run whenever the rate of media being played is changed (e.g. user switched to slow motion or fast forward mode) |
onreadystatechange | Defines the script to be run whenever the ready state of the media is changed (the ready state tracks the state of the media data). |
onseeked | Defines the script to be run when the seeking attribute has been set to FALSE i.e. the media seeking has been finished. |
onseeking | Defines the script to be run when the seeking attribute has been set to TRUE i.e. the media seeking has not finished. |
onstalled | Defines the script to be run when the browser is unable to fetch more media data for any reason. |
onsuspend | Defines the script to be run when fetching the media data is stopped before completely loading the media for any reason |
ontimeupdate | Defines the script to be run when the playing position of the media is changed (e.g. user fast forwards or rewinds to another play position in media) |
onvolumechange | Defines the script to be run when the volume of the media is changed (including muting the media) |
onwaiting | Defines the script to be run when the media is paused but will be resuming shortly (e.g. media paused for buffering) |