The DOM CustomEvent are events initialized by an application for any purpose.
Method overview
void initCustomEvent(in DOMString type, in boolean canBubble, in boolean cancelable, in any detail); |
Attributes
| Attribute | Type | Description |
|---|---|---|
detail |
any |
The data passed when initializing the event. |
Methods
initCustomEvent()
Initializes the event in a manner analogous to the similarly-named method in the DOM Events interfaces.
void initCustomEvent( in DOMString type, in boolean canBubble, in boolean cancelable, in any detail );
Parameters
-
type - The name of the event.
-
canBubble - A boolean indicating whether the event bubbles up through the DOM or not.
-
cancelable - A boolean indicating whether the event is cancelable.
-
detail - The data passed when initializing the event.
Constructor
The DOM4 specification has added support for the CustomEvent constructor.
CustomEvent( DOMString type, optional CustomEventInit eventInitDict )
Parameters
-
type - The name of the event.
-
eventInitDict - An object which provides properties for the event. Check out CustomEventInit section for details.
CustomEventInit
-
bubbles -
A boolean indicating whether the event bubbles up through the DOM or not. (default:
false) -
cancelable -
A boolean indicating whether the event is cancelable. (default:
false) -
detail - The data passed when initializing the event.
CustomEvent example usage
// add an appropriate event listener
obj.addEventListener("cat", function(e) { process(e.detail) });
// create and dispatch the event
var event = new CustomEvent("cat", {"detail":{"hazcheeseburger":true}});
obj.dispatchEvent(event);
Browser compatibility
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
|---|---|---|---|---|---|
| Basic support | (Yes) | 6 | 9 | ? | (Yes) (533.3) |
CustomEvent() constructor |
15 | 11 | Not supported | 11.60 | Nightly build (535.2) |
| Feature | Android | Firefox Mobile (Gecko) | IE Phone | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|
| Basic support | ? | ? | ? | ? | ? |
Gecko notes
It's represented by the nsIDOMCustomEvent interface, which extends the nsIDOMEvent interface.
Polyfill
You can polyfill the CustomEvent() constructor functionality in Internet Explorer 9 and 10 with the following code:
(function () {
function CustomEvent ( event, params ) {
params = params || { bubbles: false, cancelable: false, detail: undefined };
var evt = document.createEvent( 'CustomEvent' );
evt.initCustomEvent( event, params.bubbles, params.cancelable, params.detail );
return evt;
};
CustomEvent.prototype = window.CustomEvent.prototype;
window.CustomEvent = CustomEvent;
})();
Mozilla Developer Network