- ES6 - Discussion
- ES6 - Useful Resources
- ES6 - Quick Guide
- ES9 - New Features
- ES8 - New Features
- ES7 - New Features
- ES6 - Browsers
- ES6 - Image Map
- ES6 - Debugging
- ES6 - Multimedia
- ES6 - Animation
- ES6 - Validations
- ES6 - Proxy API
- ES6 - Reflect API
- ES6 - Object Extensions
- ES6 - Error Handling
- ES6 - Modules
- ES6 - Promises
- ES6 - Maps And Sets
- ES6 - Classes
- ES6 - Collections
- ES6 - Iterator
- ES6 - HTML DOM
- ES6 - RegExp
- ES6 - Math
- ES6 - Date
- ES6 - Arrays
- ES6 - New String Methods
- ES6 - Symbol
- ES6 - Strings
- ES6 - Boolean
- ES6 - Number
- ES6 - Objects
- ES6 - Page Printing
- ES6 - Void Keyword
- ES6 - Dialog Boxes
- ES6 - Page Redirect
- ES6 - Cookies
- ES6 - Events
- ES6 - Functions
- ES6 - Loops
- ES6 - Decision Making
- ES6 - Operators
- ES6 - Variables
- ES6 - Syntax
- ES6 - Environment
- ES6 - Overview
- ES6 - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
ES6 - Events
JavaScript is meant to add interactivity to your pages. JavaScript does this using a mechanism using events. Events are a part of the Document Object Model (DOM) Level 3 and every HTML element contains a set of events that can trigger JavaScript Code.
An event is an action or occurrence recognized by the software. It can be triggered by a user or the system. Some common examples of events include a user cpcking on a button, loading the web page, cpcking on a hyperpnk and so on. Following are some of the common HTML events.
Event Handlers
On the occurrence of an event, the apppcation executes a set of related tasks. The block of code that achieves this purpose is called the eventhandler. Every HTML element has a set of events associated with it. We can define how the events will be processed in JavaScript by using event handlers.
oncpck Event Type
This is the most frequently used event type which occurs when a user cpcks the left button of his mouse. You can put your vapdation, warning, etc. against this event type.
Example
<html> <head> <script type = "text/javascript"> function sayHello() { document.write ("Hello World") } </script> </head> <body> <p> Cpck the following button and see result</p> <input type = "button" oncpck = "sayHello()" value = "Say Hello" /> </body> </html>
The following output is displayed on successful execution of the above code.
onsubmitEvent Type
onsubmit is an event that occurs when you try to submit a form. You can put your form vapdation against this event type.
The following example shows how to use onsubmit. Here we are calpng a vapdate() function before submitting a form data to the webserver. If vapdate() function returns true, the form will be submitted, otherwise it will not submit the data.
Example
<html> <head> <script type = "text/javascript"> function vapdation() { all vapdation goes here ......... return either true or false } </script> </head> <body> <form method = "POST" action = "t.cgi" onsubmit = "return vapdate()"> ....... <input type = "submit" value = "Submit" /> </form> </body> </html>
onmouseover and onmouseout
These two event types will help you create nice effects with images or even with text as well. The onmouseover event triggers when you bring your mouse over any element and the onmouseout triggers when you move your mouse out from that element.
Example
<html> <head> <script type = "text/javascript"> function over() { document.write ("Mouse Over"); } function out() { document.write ("Mouse Out"); } </script> </head> <body> <p>Bring your mouse inside the spanision to see the result:</p> <span onmouseover = "over()" onmouseout = "out()"> <h2> This is inside the spanision </h2> </span> </body> </html>
The following output is displayed on successful execution of the above code.
HTML 5 Standard Events
The standard HTML 5 events are psted in the following table for your reference. The script indicates a JavaScript function to be executed against that event.
Attribute | Value | Description |
---|---|---|
offpne | script | Triggers when the document goes offpne |
onabort | script | Triggers on an abort event |
onafterprint | script | Triggers after the document is printed |
onbeforeonload | script | Triggers before the document load |
onbeforeprint | script | Triggers before the document is printed |
onblur | script | Triggers when the window loses focus |
oncanplay | script | Triggers when the media can start play, but might have to stop for buffering |
oncanplaythrough | script | Triggers when the media can be played to the end, without stopping for buffering |
onchange | script | Triggers when an element changes |
oncpck | script | Triggers on a mouse cpck |
oncontextmenu | script | Triggers when a context menu is triggered |
ondblcpck | script | Triggers on a mouse double-cpck |
ondrag | script | Triggers when an element is dragged |
ondragend | script | Triggers at the end of a drag operation |
ondragenter | script | Triggers when an element has been dragged to a vapd drop target |
ondragleave | script | Triggers when an element leaves a vapd drop target |
ondragover | script | Triggers when an element is being dragged over a vapd drop target |
ondragstart | script | Triggers at the start of a drag operation |
ondrop | script | Triggers when the dragged element is being dropped |
ondurationchange | script | Triggers when the length of the media is changed |
onemptied | script | Triggers when a media resource element suddenly becomes empty |
onended | script | Triggers when the media has reached the end |
onerror | script | Triggers when an error occurs |
onfocus | script | Triggers when the window gets focus |
onformchange | script | Triggers when a form changes |
onforminput | script | Triggers when a form gets user input |
onhaschange | script | Triggers when the document has changed |
oninput | script | Triggers when an element gets user input |
oninvapd | script | Triggers when an element is invapd |
onkeydown | script | Triggers when a key is pressed |
onkeypress | script | Triggers when a key is pressed and released |
onkeyup | script | Triggers when a key is released |
onload | script | Triggers when the document loads |
onloadeddata | script | Triggers when media data is loaded |
onloadedmetadata | script | Triggers when the duration and other media data of a media element is loaded |
onloadstart | script | Triggers when the browser starts to load the media data |
onmessage | script | Triggers when the message is triggered |
onmousedown | script | Triggers when a mouse button is pressed |
onmousemove | script | Triggers when the mouse pointer moves |
onmouseout | script | Triggers when the mouse pointer moves out of an element |
onmouseover | script | Triggers when the mouse pointer moves over an element |
onmouseup | script | Triggers when a mouse button is released |
onmousewheel | script | Triggers when the mouse wheel is being rotated |
onoffpne | script | Triggers when the document goes offpne |
ononpne | script | Triggers when the document comes onpne |
onpagehide | script | Triggers when the window is hidden |
onpageshow | script | Triggers when the window becomes visible |
onpause | script | Triggers when the media data is paused |
onplay | script | Triggers when the media data is going to start playing |
onplaying | script | Triggers when the media data has start playing |
onpopstate | script | Triggers when the window s history changes |
onprogress | script | Triggers when the browser is fetching the media data |
onratechange | script | Triggers when the media data s playing rate has changed |
onreadystatechange | script | Triggers when the ready-state changes |
onredo | script | Triggers when the document performs a redo |
onresize | script | Triggers when the window is resized |
onscroll | script | Triggers when an element s scrollbar is being scrolled |
onseeked | script | Triggers when a media element s seeking attribute is no longer true, and the seeking has ended |
onseeking | script | Triggers when a media element s seeking attribute is true, and the seeking has begun |
onselect | script | Triggers when an element is selected |
onstalled | script | Triggers when there is an error in fetching media data |
onstorage | script | Triggers when a document loads |
onsubmit | script | Triggers when a form is submitted |
onsuspend | script | Triggers when the browser has been fetching media data, but stopped before the entire media file was fetched |
ontimeupdate | script | Triggers when the media changes its playing position |
onundo | script | Triggers when a document performs an undo |
onunload | script | Triggers when the user leaves the document |
onvolumechange | script | Triggers when the media changes the volume, also when the volume is set to "mute" |
onwaiting | script | Triggers when the media has stopped playing, but is expected to resume |