Paul said:
btw...yes, my hope was something like...
if ( !window.addEventListener && window.attachEvent )
Node.prototype.addEventListener = function(element, func) {
window.attachEvent(element, func);
}
but that's still probably wrong syntax and i'd appreciate correction. thx
guys. ( should it be return window.attachEvent, ect. )
There are more problems than simply make a copy of a function under
another name.
1) addEventListener requires three arguments: event name, function
reference and capturing phase
attachEvent has only two arguments: event name and function
reference.
2) addEventListener wants all event names in "pure" form (w/o "on"):
"click", "load".
attachEvent wants all event names in "on" form: "onclick", "onload".
As it was explained before you cannot extend the prototype of every
single HTML element at once any way.
I'm using sometimes (when events are getting to messy) this self-made
event dispatcher. The other benefit is that it will normalise event
names so can use any form you prefer (with "on" or w/o "on"):
function EventDispatcher() {
this.getClassName = function() {return 'EventDispatcher';}
this.addListener = function(obj, evt, fun, b) {
var result = true;
var eventPhase = (typeof(b)=='boolean') ? b : false;
var eventName = normaliseEventName(evt,obj);
if ('addEventListener' in obj) {
obj.addEventListener(eventName, fun, eventPhase);
}
else if ('attachEvent' in obj) {
obj.attachEvent(eventName, fun);
}
else {
result = false;
}
return result;
}
function normaliseEventName(eName, context) {
var rightName = eName.toLowerCase();
if (('addEventListener' in context)&&
(rightName.charAt(0) == 'o')) {
rightName = rightName.substring(2);
}
else if (('attachEvent' in context)&&
(rightName.charAt(0) != 'o')) {
rightName = 'on' + rightName;
}
else {
/*NOP*/
}
return rightName;
}
}
and then later:
var Dispatcher = new EventDispatcher();
Dispatcher.addListener(obj,'onclick',foo);
// or Dispatcher.addListener(obj,'click',foo);
If you find it anyhow useful, you're welcome to extend the prototype
with
this.removeListener