Sponsored Links
-->

Tuesday, December 12, 2017

mixonic on Twitter:
src: pbs.twimg.com

DOM (Document Object Model) events allow event-driven programming languages like JavaScript, JScript, ECMAScript, VBScript and Java to register various event handlers or listeners on the element nodes inside a DOM tree, such as in HTML, XHTML, XUL and SVG documents.

Historically, like DOM, the event models used by various web browsers had some significant differences. This caused compatibility problems. To combat this, the event model was standardized by the World Wide Web Consortium (W3C) in DOM Level 2.


Video DOM events



Events

HTML events

Common events

There is a huge collection of events that can be generated by most element nodes:

  • Mouse events
  • Keyboard events
  • HTML frame/object events
  • HTML form events
  • User interface events
  • Mutation events (notification of any changes to the structure of a document)
  • Progress events (used by XMLHttpRequest, File API, ...)

Note that the event classification above is not exactly the same as W3C's classification.

Note that the events whose names start with "DOM" are currently not well supported, and for this and other performance reasons are deprecated by the W3C in DOM Level 3. Mozilla and Opera support DOMAttrModified, DOMNodeInserted, DOMNodeRemoved and DOMCharacterDataModified. Chrome and Safari support these events, except for DOMAttrModified.

Touch events

Web browsers running on touch-enabled devices, such as Apple's iOS and Google's Android, generate additional events.

In the W3C draft recommendation, a TouchEvent delivers a TouchList of Touch locations, the modifier keys that were active, a TouchList of Touch locations within the targeted DOM element, and a TouchList of Touch locations that have changed since the previous TouchEvent.

Apple didn't join this working group, and delayed W3C recommendation of its Touch Events Specification by disclosing patents late in the recommendation process.

Pointer events

Web browsers on devices with various types of input devices including mouse, touch panel, and pen may generate integrated input events. Users can see what type of input device is pressed, what button is pressed on that device, and how strongly the button is pressed when it comes to a stylus pen. As of October 2013, this event is only supported by Internet Explorer 10 and 11.

Indie UI events

Not yet really implemented, the Indie UI working groups want to help web application developers to be able to support standard user interaction events without having to handle different platform specific technical events that could match with it.

Scripting usable interfaces can be difficult, especially when one considers that user interface design patterns differ across software platforms, hardware, and locales, and that those interactions can be further customized based on personal preference. Individuals are accustomed to the way the interface works on their own system, and their preferred interface frequently differs from that of the web application author's preferred interface.

For example, web application authors, wishing to intercept a user's intent to 'undo' the last action, need to "listen" for all the following events:

  • control+z on Windows and Linux.
  • command+z on Mac OS X.
  • Shake events on some mobile devices.

It would be simpler to listen for a single, normalized request to 'undo' the previous action.

Internet Explorer-specific events

In addition to the common/W3C events, two major types of events are added by Internet Explorer. Some of the events have been implemented as de facto standards by other browsers.

  • Clipboard events
  • Data binding events

Note that Mozilla, Safari and Opera also support readystatechange event for the XMLHttpRequest object. Mozilla also supports the beforeunload event using traditional event registration method (DOM Level 0). Mozilla and Safari also support contextmenu, but Internet Explorer for the Mac does not.

Note that Firefox 6 and later support beforeprint and afterprint events.

XUL events

In addition to the common/W3C events, Mozilla defined a set of events that work only with XUL elements.

Other events

For Mozilla and Opera 9, there are also undocumented events known as "DOMContentLoaded" and "DOMFrameContentLoaded" which fire when the DOM content is loaded. These are different from "load" as they fire before the loading of related files (e.g., images). However, DOMContentLoaded has been added to the HTML 5 specification. The "DOMContentLoaded" event was also implemented in the Webkit rendering engine build 500+. This correlates to all versions of Google Chrome and Safari 3.1+. DOMContentLoaded is also implemented in Internet Explorer 9.

Opera 9 also supports the Web Forms 2.0 events "DOMControlValueChanged", "invalid", "forminput" and "formchange".


Maps DOM events



Event flow

Consider the situation when there are 2 elements nested together. Both have event handlers registered on the same event type, say "click". When the user clicks on the inner element, there are two possible ways to handle it:

  • Trigger the elements from outer to inner (event capturing). This model is implemented in Netscape Navigator.
  • Trigger the elements from inner to outer (event bubbling). This model is implemented in Internet Explorer and other browsers.

W3C takes a middle position in this struggle. 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 Internet Explorer), and/or by cancelling the default action for the event.


JavaScript Fundamental Course (DOM Events) 5.5 - Bangla Tutorial ...
src: i.ytimg.com


Event object

The Event object provides a lot of information about a particular event, including information about target element, key pressed, mouse button pressed, mouse position, etc. Unfortunately, there are very serious browser incompatibilities in this area. Hence only the W3C Event object is discussed in this article.


Introduction to JavaScript - ppt video online download
src: slideplayer.com


Event handling models

DOM Level 0

This event handling model was introduced by Netscape Navigator, and remains the most cross-browser model as of 2005. There are two model types: inline model and traditional model.

Inline model

In the inline model, event handlers are added as attributes of elements. In the example below, an alert dialog box with the message "Hey Joe" appears after the hyperlink is clicked. The default click action is cancelled by returning false in the event handler.

One common misconception with the inline model is the belief that it allows the registration of event handlers with custom arguments, e.g. name in the triggerAlert function. While it may seem like that is the case in the example above, what is really happening is that the JavaScript engine of the browser creates an anonymous function containing the statements in the onclick attribute. The onclick handler of the element would be bound to the following anonymous function:

This limitation of the JavaScript event model is usually overcome by assigning attributes to the function object of the event handler or by using closures.

Traditional model

In the traditional model, event handlers can be added/removed by scripts. Like the inline model, each event can only have one event handler registered. The event is added by assigning the handler name to the event property of the element object. To remove an event handler, simply set the property to null:

To add parameters:

Inner functions preserve their scope.

DOM Level 2

The W3C designed a more flexible event handling model in DOM Level 2.

Some useful things to know :

  • To prevent an event from bubbling, developers must call the "stopPropagation()" method of the event object.
  • To prevent the default action of the event to be called, developers must call the "preventDefault" method of the event object.

The main difference from the traditional model is that multiple event handlers can be registered for the same event. The useCapture option can also be used to specify that the handler should be called in the capture phase instead of the bubbling phase. This model is supported by Mozilla, Opera, Safari, Chrome and Konqueror.

A rewrite of the example used in the traditional model

Internet Explorer-specific model

Microsoft Internet Explorer prior to version 8 does not follow the W3C model, as its own model was created prior to the ratification of the W3C standard. Internet Explorer 9 follows DOM level 3 events, and Internet Explorer 11 deletes its support for Microsoft-specific model.

Some useful things to know :

  • To prevent an event bubbling, developers must set the event's cancelBubble property.
  • To prevent the default action of the event to be called, developers must set the event's "returnValue" property.
  • The this keyword refers to the global window object.

Again, this model differs from the traditional model in that multiple event handlers can be registered for the same event. However the useCapture option can not be used to specify that the handler should be called in the capture phase. This model is supported by Microsoft Internet Explorer and Trident based browsers (e.g. Maxthon, Avant Browser).

A rewrite of the example used in the old Internet Explorer-specific model


JavaScript Tutorial - DOM Events - part 1 - Basics - YouTube
src: i.ytimg.com


References

  • Deitel, Harvey. (2002). Internet and World Wide Web: how to program (Second Edition). ISBN 0-13-030897-8
  • The Mozilla Organization. (2009). DOM Event Reference. Retrieved August 25, 2009.
  • Quirksmode (2008). Event compatibility tables. Retrieved November 27, 2008.
  • http://www.sitepen.com/blog/2008/07/10/touching-and-gesturing-on-the-iphone/

DOM Animation Changing the DOM objects over time. - ppt download
src: images.slideplayer.com


External links

  • Document Object Model (DOM) Level 2 Events Specification
  • Document Object Model (DOM) Level 3 Events Working Draft
  • DOM4: Events (Editor's Draft)
  • UI Events Working Draft
  • Pointer Events W3C Candidate Recommendation
  • MSDN PointerEvent
  • JS fiddle for Event Bubbling and Capturing

The Rolling Scopes School: DOM Events - YouTube
src: i.ytimg.com


References

Source of article : Wikipedia