Crossbrowser events


Events, the center pillarstone of any javascript application nowadays. Still, IE keeps doing it their own way.

Of course you can easily map the attachEvent to addEventListener, but that still leaves this as the window object. This fixes that problem:

Code: (javascript)
global.on = function(strEvent, objElement, func){
if (!window.addEventListener) {
objElement.attachEvent('on'+strEvent, function(){, event, true);});
} else {
objElement.addEventListener(strEvent, func, false);
return this;

Now all you have to do is call:

Code: (javascript)
on(body, 'click', function(){
alert("Clicked body! "+this);

To attach the function to the body. Of course, there's no bubbles... But at least there's a proper this. As an added bonus, the event argument will always be filled by the event object (it normally isn't in explorer). And a second parameter is returned that indicates IE event (true) or not (undefined).

Hope it helps you :)