PDA

View Full Version : Event Listener Function - Help me understand.


saintseiya
04-25-2007, 05:20 PM
Hey, thanks in advance to anyone who offers any kind of explanation to my question :)

I have this piece of code:
function listen(event, elem, func) {
if (elem.addEventListener) // W3C DOM
elem.addEventListener(event,func,false);
else if (elem.attachEvent) // IE DOM
elem.attachEvent('on'+event, function(){ func(new W3CDOM_Event(elem)) } );
// for IE we use a wrapper function that passes in a simplified faux Event object.
else throw 'cannot add event listener';
}

function W3CDOM_Event(currentTarget) {
this.currentTarget = currentTarget;
this.preventDefault = function() { window.event.returnValue = false }
return this;
}

I dont understand why for IE it needs to call the W3CDOM_Event function instead of just putting the event as the second parameter.

Any help appreciated, thanks :)

david_kw
04-25-2007, 06:08 PM
That is because the function parameter func expects the event passed in to it to conform in some way to W3C standard. Honestly there are a lot more difference than the code is addressing so to me its of minimal use. But as long as those are the only differences you need for the func() parameter then it should be ok.

In short, the code is trying to create a consistant event so the func() parameter doesn't have to do the browser specific code.

david_kw