Skip to main content


Showing posts from 2009

Quick Event-Handling Suggestion

Today I had to rewrite how some events were being handled in an application. One of the advantages often cited when using DOM level 2 event handling (use of attachEvent or addEventListener) is that you can add multiple handlers for the same event type on the same element. While I do not deny this to be an advantage (I will soon explain why), I feel that this practice should generally be avoided. A couple reasons, 1.) browsers do no guarantee what order the events get fired in (see here) 2.) Even if you're using a framework that ensures the events are fired in a specific order, you may still run into issues with how one handler should possibly not be fired due to certain circumstances.

When do you make use of multiple handles for the same type/element pair? I would say generally only when the events are between separate objects. To keep things modular, it is best to let each object/class handle the events by itself rather than trying to make some super handler.

Var's and closures

I have a tendency at times to take the 'var' statement as "Ok, now that we have reached this line, lets create this variable with this name". In reality though, it serves more for an identification than a procedural statement. To try to explain better, this is an issue I came across:varsomeVar=5;varrunMe=function(){alert(someVar);varsomeVar=6;alert(someVar);}runMe();Now, I thought this would alert '5' then '6'. However, it alerts 'undefined' and '6'. The Ecma-262 specification says the following in 12.2:Variables are created when the execution scope is entered. A Block does not define a new execution scope. Only Program and FunctionDeclaration produce a new scope. Variables are initialised to undefined when created. A variable with an Initialiser is assigned the value of its AssignmentExpression when the VariableStatement is executed, not when the variable is created.If I'm reading this correctly, this means that the variable &…

IE Oddities

I recently ran into an extremely frustrating glitch with some web pages I was working on. To spare myself more repetition, below is the description of the problem that I almost posted to a Google Group in search of an answer as I had run out of ideas:

I have an extremely strange issue with a page I'm working on. For some reason, on this one machine that I remote into, the inline event handlers inside a particular table fail to work. I know the ideal thing here is to get rid of them and go with DOM level 2 but that would take some considerable effort and I really need a short term solution at the moment. This problem only happens on one particular machine (just so happens the one people have to use) that we'll name "Weirdo". I can run the same code locally and it works fine, I can even load the site from Weirdo onto another remote machine or my local machine and it works. It's only on Weirdo and only in rows of a particular table do inline event handlers fail…