The squatter is currently hosting my copyrighted material. I wrote to the registrar to complain but haven’t received a reply (I should probably contact the hosting service too).

So, if you could update your backlinks/bookmarks etc that would be cool. I know that it’s my fault though.

The old domain was actually worth quite a lot to me. It provided my email address which was the way that people contacted me for work. That email address has provided all of my work for the last five years. There’s no way I can replace it. I’ll just have to start again.

Update: I’m now in contact with the new owner of the domain and it looks like it may be returned to me. Fingers crossed!

Most of the major JavaScript libraries claim to support custom events in one form or another.
For example, jQuery, YUI and Dojo all support a custom “document ready” event. However, the implementation
of these custom events is always some form of callback system.

A callback system works by storing event handlers in an array. When the underlying event is detected the dispatch system loops through the array calling the callback functions in turn. So what’s wrong with that? Before I answer that, let’s look at some code.

Here is some simple code that uses the DOMContentLoaded event to perform two
separate initialisations:

The problem is clear. Callback systems are brittle. If any of the callback functions throw
an error then the subsequent callbacks are not executed. In reality, this means that a poorly written
plugin can prevent other plugins from initialising.

Dojo suffers exactly the same problem as jQuery. The YUI library takes a slightly different approach.
It wraps a try/catch around its dispatch mechanism. The downside is that your errors occur
silently:

Perfect initialisation! Nothing to worry about here! Except for the error that you don’t see.

So what’s the solution?

The Solution

The solution is to use a hybrid of a callback system and real event dispatch.
We can trigger a fake event and from within that event, run the callback function.
Each event handler has its own execution context. If an error occurs in our fake event
then it won’t affect our callback system.

Perfect! Just what we want. Both event handlers are executed and we also get a message
telling us about the error in the first handler. Great!

But what about Internet Explorer I hear you ask (I have good hearing). MSIE does not support
the standard event dispatch system. It has its own method; fireEvent but that only
works with real events (e.g. click).

A similar approach except that we use the proprietary propertychange event as the trigger.

Summary

I’ve shown a very simple example of how to use the uderlying event system to fire custom
events. Library authors should be capable of seeing how this can be extended to fully support
cross-browser custom events.

Update

Some commenters have suggested using setTimeout. Here is my response to that:

For this particular example, a timer will work fine. This is just an example to illustrate the technique. The real usefulness of this is for other custom events. Most libraries implement custom events using a callback system. As I illustrated, callback systems are brittle. Dispatching events with timers will work to a degree but it is not how a real event system works. In a real system, events are dispatched sequentially. There are other concerns, like cancelling an event and stopping the event from bubbling. This would be impossible with timers.

The important thing is that I’ve demonstrated a technique for wrapping a callback system in a real event dispatch system. That gives you the ability to fire real custom events in MSIE. If you are building an event system based on event delegation then this technique may also be interesting to you.

Update 2

It seems that Prototype uses an almost identical trick to fire custom events in MSIE: