Anne van Kesteren

Simplified initializing of synthetic events

The DOM event model allows authors to listen and act on user-agent generated events. It also provides a way for authors to dispatch their own events. Typically this is known as dispatching synthetic events. Here is an example:

var ev = document.createEvent("UIEvent")
ev.initUIEvent("test", false, false, null, 1) // obsolete
document.dispatchEvent(ev)

With the current design each event with a specialized interface has its own initInterfaceName() method. Sometimes with a lot of arguments of which it is hard to remember the order. Another problem is that if a specification later wants to change the event interface to support more features the initialization method also needs to be changed creating a backwards compatibility problem.

To address this DOM Core introduces a new init() method. In turn, this method makes use of a new concept in Web IDL: dictionaries. The second line in the above example can now be rewritten as follows:

ev.init("test", {view:null, detail:1})

While this is slightly more verbose, it is much clearer, more extensible, and allows for omitting arguments if they are set to their default value. Unless there are unforeseen problems with this approach we can hopefully roll it out relatively fast in implementations and adopt it throughout the web platform, replacing the initInterfaceName() methods.

Going forward we might make dispatching synthetic events easier. E.g. by providing a method that does the above three steps as one. You can provide feedback on www-dom@w3.org; much appreciated!