The Problem With Stacking Browser Event Handlers From JavaScript

You learn something new every day, which is why I love being alive.

Its an astounding coincidence that just when I needed to implement a cross browser method of stacking event handlers (for MetaWrap WireWrap), this blog post came out describing exactly how you should not do it.

The issue seemed to be the difference between browsers that implement the W3C spec vs Microsoft’s spec for dynamically adding an event handler.

The key difference is that the this keyword does not work properly in Microsoft’s attachEvent. Instead of referring to the element the event handler is defined on, as it does in the W3C model, it refers to the window object.

There is also a memory leak in IE which for which the only solution is to garbage collect ever event handler you ever created, for example – on body.onunload() killing all references to event handlers. This leak is probably based on circular references.

So I’m going to write an event registration class in JavaScript that deals with both of these problems.

Related to WireWrap, it was pointed out to me that yet another CSS query/select JavaScript class has been released in this blog post which seems to implement the full set of CSS selectors.

Damned if I’m going to write my own now.

cssQuery() is a powerful cross-browser JavaScript function that enables querying of a DOM document using CSS selectors. All CSS1 and CSS2 selectors are allowed plus quite a few CSS3 selectors.

About James McParlane

CTO Massive Interactive. Ex Computer Whiz Kid - Now Grumpy Old Guru.
This entry was posted in Coolhunting, JavaScript. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s