17.1.3 HTML5 Events
17.1.3 HTML5 Events
HTML5 and related standards define a host of new APIs for web applications (see Chapter 22 ). Many of these APIs define events. This section lists and briefly describes these HTML5 and web application events. Some of these events are ready to be used now and are explained in more detail elsewhere in the book. Others are not yet widely implemented and are not documented in any detail.
One of the widely advertised features of HTML is inclusion ofandelements for playing sound and video. These elements have a long list of events that they trigger to send notifications about network events, data buffering status, and
These media events are passed an ordinary event object with no special properties. The target property identifies theorelement, however, and that element has many relevant properties and methods. See §21.2 for more details on these elements, their properties, and their events.
dragstart drag dragend dragenter dragover dragleave drop
These drag-and-drop events are triggered with an event object like those sent with mouse events. One additional property, dataTransfer, holds a DataTransfer object that contains information about the data being transferred and the formats in which it is available. The HTML5 drag-and-drop API is explained and demonstrated in §17.7 .
HTML5 defines a history management mechanism ( §22.2 ) that allows web applications to interact with the browser’s Back and Forward buttons. This mechanism involves events named hashchange and popstate. These events are lifecycle notification events like load and unload and are fired at the Window object rather than any individual document element.
HTML5 defines a lot of new features for HTML forms. In addition to standardizing the form input event described earlier, HTML5 also defines a form validation mechanism, which includes an invalid event fired on form elements that have failed validation. Browser vendors other than Opera have been slow to implement HTML5’s new form features and events, however, and this book does not cover them.
HTML5 includes support for offline web applications (see §20.4 ) that can be installed locally in an application cache so that they can run even when the browser is offline (as when a mobile device is out of network range). The two most important events associated with this are the offline and online events: they are triggered on the Window object whenever the browser loses or gains a network connection. A number of additional events are defined to provide notification of application download progress and application cache updates:
cached checking downloading error noupdate obsolete progress updateready
A number of new web application APIs use a message event for asynchronous communication. The Cross-Document Messaging API ( §22.3 ) allows scripts in a document from one server to exchange messages with scripts in a document from another server. This works around the limitations of the same-origin policy ( §13.6.2 ) in a secure way. Each message that is sent triggers a message event on the Window of the receiving document. The event object passed to the handler includes a data property that holds the content of the message as well as source and origin policies that identify the sender of the message. The message event is used in similar ways for communication with Web Workers ( §22.4 ) and for network communication via Server-Sent Events ( §18.3 ) and WebSockets ( §22.9 ).
HTML5 and related standards define some events that are triggered on objects other than windows, documents, and document elements. Version 2 of the XMLHttpRequest specification, as well as the File API specification, define a series of events that track the progress of asynchronous I/O. They trigger events on an XMLHttpRequest or FileReader object. Each read operation begins with a loadstart event, followed by progress events and a loadend event. Additionally, each operation ends with a load, error, or abort event just before the final loadend event. See §18.1.4 and §22.6.5 for details.
Finally, HTML5 and related standards define a few miscellaneous event types. The Web Storage ( §20.1 ) API defines a storage event (on the Window object) that provides notification of changes to stored data. HTML5 also standardizes the beforeprint and afterprint events that were originally introduced by Microsoft in IE. As their names imply, these events are triggered on a Window immediately before and immediately after its document is printed and provide an opportunity to add or remove content such as the date and time that the document was printed. (These events should not be used to change the presentation of a document for printing because CSS media types already exist for that purpose.)
17.1 Types of Events | 455欢迎转载,转载请注明来自一手册:http://yishouce.com/book/1/27973.html