Advanced JavaScript by Zachary Shute
Author:Zachary Shute
Language: eng
Format: epub, azw3, mobi
Publisher: Packt Publishing
Published: 2019-01-30T16:00:00+00:00
Note
The event constructor works will all modern browsers except for Internet Explorer. For full compatibility with IE, you must use the createEvent() and initEvent() methods discussed later, or use a polyfill to simulate the CustomEvent class.
To maximize code browser compatibility, we must also discuss the initEvent() and createEvent() methods for creating custom events. These methods are deprecated and have been removed from the web standard. Some browsers, however, still support these functions. To create a custom event in an older browser, you must first create the event with var event = document.createEvent( 'Event' ) (we must use var instead of const in old browsers) and then initialize the new event with event.initEvent(). CreateEvent() takes in a single argument, type. This is the type of event object that will be created. This type must be one of the standard JavaScript event types, such as Event, MouseEvent, and so on. InitEvent() takes in three arguments. The first argument is a string that represents the type name of the event. For example, a click event's type is click. The second argument is a Boolean that represents the event's bubble behavior. The third argument is a Boolean that represents the event's cancelable behavior. These two behaviors were discussed in the Event Objects and Handling Events section of this topic.
To catch and handle custom events, we can use the standard event listener behaviors. All we need to do is attach an event listener with addEventListener() that listens for the custom event type that's been added. For example, if we create a CustomEvent with the event type myEvent, all we need to do to listen for that event is add an event listener on that type with addEventListener( 'myEvent', e => {} ). Whenever an event with the type myEvent is fired, the added event listener callback will be called.
When the event listener callback is called, the event parameter in the callback will have an additional field, detail. This field will contain the information passed in to the custom event through the detail field of the custom event options object. Any information that's relevant to the custom event should be passed through the detail object. An example of the detail object is shown in the following snippet:
const element = document.getElementById( 'button' );
element.addEventListener( 'myClick', e => {
console.log( e.detail );
} );
const event = new CustomEvent( 'myClick' , { detail: 'Hello!' } );
const canceled = element.dispatchEvent( event );
Download
Advanced JavaScript by Zachary Shute.azw3
Advanced JavaScript by Zachary Shute.mobi
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Coding Theory | Localization |
Logic | Object-Oriented Design |
Performance Optimization | Quality Control |
Reengineering | Robohelp |
Software Development | Software Reuse |
Structured Design | Testing |
Tools | UML |
Deep Learning with Python by François Chollet(12566)
Hello! Python by Anthony Briggs(9911)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9795)
The Mikado Method by Ola Ellnestam Daniel Brolund(9775)
Dependency Injection in .NET by Mark Seemann(9336)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8293)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7758)
Grails in Action by Glen Smith Peter Ledbrook(7693)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7557)
Becoming a Dynamics 365 Finance and Supply Chain Solution Architect by Brent Dawson(7026)
Microservices with Go by Alexander Shuiskov(6793)
Practical Design Patterns for Java Developers by Miroslav Wengner(6705)
Test Automation Engineering Handbook by Manikandan Sambamurthy(6647)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6409)
Angular Projects - Third Edition by Aristeidis Bampakos(6054)
The Art of Crafting User Stories by The Art of Crafting User Stories(5585)
NetSuite for Consultants - Second Edition by Peter Ries(5519)
Demystifying Cryptography with OpenSSL 3.0 by Alexei Khlebnikov(5321)
Kotlin in Action by Dmitry Jemerov(5061)
