Cross-browser event handling

Share this article

One of the most important aspects of modern unobtrusive DHTML is dealing with events. An event occurs whenever the user interacts with the page in some way – by clicking a link, moving their mouse or typing on the keyboard for example. JavaScript programmers write code to respond to those events, and attach them to specific parts of the page using event handlers.

Sadly, while the modern set of browsers all provide excellent support for most parts of the W3C’s DOM specification, event handling is the one place where significant differences between Internet Explorer and Mozilla/Safari/Opera still exists. Microsoft are not entirely to blame for this: at the time IE 5 was released, the W3C specification for DOM events had not yet been finalised. Hopefully, IE 7 will support the specification in full in addition to maintaining backwards compatibility with previous versions of IE.

In the meantime, workarounds are needed. Thankfully, there’s plenty of documentation to fill the gap. Todd Ditchendorf recently published two articles on this subject: Equalizing JavaScript Event References across IE and EOMB (Every Other Modern Browser) Part 1: DOM Level 0 Event Handlers and Part 2: Programatically registering Event Handlers. Meanwhile, Scott Andrew’s tried and tested addEvent function has been helping deal with the problem since 2001. PPK’s QuirksMode provides extensive coverage of the various browser event models in existence.

This topic is also covered in detail in Stuart’s upcoming book.

Simon WillisonSimon Willison
View Author
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week
Loading form