Javascript Events
When user performs any action on HTML DOM element ,there could be certain events to be performed based on parent/child level.
Event Order
Element1 has Element2 in it, when we click on Element2 ,i.e Element2 is child for Element1 ,We should perform two events
- Element2 click event
- Element1 click event.
There should be a order for firing click event on these two elements.
- Trigger the elements from outer to inner ,Element1 first,Element2 second,(event capturing). This model is implemented in Netscape Navigator.
- Trigger the elements from inner to outer,Element2 first,Element1 ,(event bubbling). This model is implemented in Internet Explorer and other browsers.
as per W3C Events are first captured until it reaches the target element, and then bubbled up. During the event flow, an event can be responded to at any element in the path (an observer) in either phase by causing an action, and/or by stopping the event (with method event.stopPropagation() for W3C-conforming browsers and command event.cancelBubble = true for IE), and/or by cancelling the default action for the event.
Since Event is a Object in Javascript, it has following properties and methods in it
Event Properties
| Type | Name | Description |
|---|---|---|
| DOMString | type | The name of the event (case-insensitive). |
| EventTarget | target | Used to indicate the EventTarget to which the event was originally dispatched. |
| EventTarget | currentTarget | Used to indicate the EventTarget whose EventListeners are currently being processed. |
| unsigned short | eventPhase | Used to indicate which phase of event flow is currently being evaluated. |
| boolean | bubbles | Used to indicate whether or not an event is a bubbling event. |
| boolean | cancelable | Used to indicate whether or not an event can have its default action prevented. |
| DOMTimeStamp | timeStamp | Used to specify the time (in milliseconds relative to the epoch) at which the event was created. |
Event Methods
| Name | Argument type | Argument name | Description |
|---|---|---|---|
| stopPropagation | To prevent further propagation of an event during event flow. | ||
| preventDefault | To cancel the event if it is cancelable, meaning that any default action normally taken by the implementation as a result of the event will not occur. | ||
| initEvent | DOMString | eventTypeArg | Specifies the event type. |
| boolean | canBubbleArg | Specifies whether or not the event can bubble. | |
| boolean | cancelableArg | Specifies whether or not the event’s default action can be prevented. |
Preventing bubble and cancelling events
- To prevent an event from bubbling, we must call the "stopPropagation()" method of the event object.
- To prevent the default action of the event to be called, we must call the "preventDefault()" method of the event object.
Attaching Events as per DOM2
Microsoft IE does not follow the W3C model up until Internet Explorer 8, as its own model was created prior to the ratification of the W3C standard.
W3C model event manipulation methods
| Name | Description | Argument type | Argument name |
|---|---|---|---|
| addEventListener | Allows the registration of event listeners on the event target. | DOMString | type |
| EventListener | listener | ||
| boolean | useCapture | ||
| removeEventListener | Allows the removal of event listeners from the event target. | DOMString | type |
| EventListener | listener | ||
| boolean | useCapture | ||
| dispatchEvent | Allows to send the event to the subscribed event listeners. | Event | evt |
Mircrosoft Internet Explorer <8 ,event manipulation methods
| Name | Description | Argument type | Argument name |
|---|---|---|---|
| attachEvent | like W3C’s addEventListener method. | String | sEvent |
| Pointer | fpNotify | ||
| detachEvent | like W3C’s removeEventListener method. | String | sEvent |
| Pointer | fpNotify | ||
| fireEvent | like W3C’s dispatchEvent method. | String | sEvent |
| Event | oEventObject |
Cross-Browser Events and Their Bubble and Cancelable Status
| Event | Bubbles | Cancel |
|---|---|---|
| onabort | No | No |
| onblur | No | No |
| onchange | Internet Explorer—No,Mozilla—Yes | Internet Explorer—Yes Mozilla—No |
| onclick | Yes | Yes |
| ondblclick | Yes | Yes |
| onerror | No | Yes |
| onfocus | No | No |
| onkeydown | Yes | Yes |
| onkeypress | Yes | Yes |
| onkeyup | Yes | Yes |
| onload | No | No |
| onmousdown | Yes | Yes |
| onmousmove | Yes | No |
| onmouseout | Yes | Yes |
| onmouseover | Yes | Yes |
| onmouseup | Yes | Yes |
| onmove | Yes | No |
| onreset | No | Yes |
| onresize | Yes | No |
| onsubmit | Internet Explorer—No Mozilla—Yes | Yes |
| onunload | No | No |

