Skip to content

Instantly share code, notes, and snippets.

@boopathi
Created April 25, 2011 06:26
Show Gist options
  • Save boopathi/940232 to your computer and use it in GitHub Desktop.
Save boopathi/940232 to your computer and use it in GitHub Desktop.
cross-browser addEvent
Object.prototype.addEvent = function(evt, handler, useCapt) {
//JUST A CHECK TO HANDLE “onclick” and “click” as evt
if(evt.match(“^on”))
evt = evt.substr(2);
if(this.attachEvent)
return this.attachEvent('on' + evt, handler); // FOR IE
else if(this.addEventListener)
return this.addEventListener(evt, handler, useCapt); // OTHERS
else {
//IF BOTH FAILS
this[‘on’+evt] = handler;
return this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment