CustomEvent

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.Event.prototype;

  window.CustomEvent = CustomEvent;
})();

Firing from privileged code to non-privileged code

When firing a CustomEvent from privileged code (i.e. an extension) to non-privileged code (i.e. a webpage), you must take security into consideration. Firefox and other Gecko applications restrict an object created in one context from being directly used from another, which will generally automatically prevent security holes, but these restrictions may also prevent your code from running as expected.

When creating a CustomEvent object, you must create the object from the same window as you're going to fire against.

// doc is a reference to the content document
function dispatchCustomEvent(doc) {
  // This will not work. CustomEvent will be created from the chrome window and will not be seen by the content.
  // var myEvent = new CustomEvent("mytype");
  // Create CustomEvent from the content window
  var myEvent = doc.defaultView.CustomEvent("mytype");
  doc.dispatchEvent(myEvent);
}

The detail attribute of your CustomEvent will be subject to the same restrictions. String and Array values will be readable by the content without restrictions, but custom Objects will not. If using a custom Object, you will need to define the attributes of that object that are readable from the content script using __exposedProps__.

// doc is a reference to the content document
function dispatchCustomEvent(doc) {
  var eventDetail = {foo: 'bar', __exposedProps__ : { foo : "r"}};
  var myEvent = doc.defaultView.CustomEvent("mytype", eventDetail);
  doc.dispatchEvent(myEvent);
}

Note that exposing a function will allow the content script to run it with chrome privileges, which can open a security vulnerability.

See also

Specification

Document Tags and Contributors

Last updated by: mereskin,