Version: (using KDE 4.0.0) Installed from: Gentoo Packages I use the following code in my AJAX webapp to determine the best method for trapping events. However, in KDE 4.0.1, the 3rd DOM-0 case is used, despite window.addEventListener evaluating to a Function object in the debugger at the time of the check (script startup). if (window.addEventListener) { function trapEvent(o, t, func) { o.addEventListener(t, func, false); } } else if (window.attachEvent) { function trapEvent(o, t, func) { o.attachEvent('on' + t, func); } } else { function trapEvent(o, t, func) { o['on' + t] = func; } }
You're relying on a mozilla standards non-compliance there. There was a bug in KJS 3.x that made it sort-of-compatible with it, but it's now been changed to follow the standard, like every other web browser out there. Function declarations are specified by ECMA-262-3 to be processed at function entry time, the conditionals don't matter at all. See sections 10.1.3 and 13. What you want to do is this: if (window.addEventListener) { var trapEvent = function trapEvent(o, t, func) { ... etc.
Wouldn't that make 'trapEvent' scoped to just that 'if' statement?
No. There are no local scopes in ECMAScript[1]. Anything defined with 'var' is scoped to the function. [1] Mozilla's JavaScript has non-standard extensions for those, of course.