Skip to content

Instantly share code, notes, and snippets.

@livingston
Created June 8, 2010 19:05
Show Gist options
  • Save livingston/430487 to your computer and use it in GitHub Desktop.
Save livingston/430487 to your computer and use it in GitHub Desktop.
Cross-browser Compatible Bind
/* Cross-browser Compatible Event Bind Method
*
* @arguments element reference, event name, function/function reference
*
*/
var bind = (function (win) {
if (win.addEventListener) {
return function (elem, evt, fn, phase) {
elem.addEventListener(evt, fn, phase || false);
};
} else if (win.attachEvent) {
return function (elem, evt, fn) {
elem.attachEvent('on' + evt, fn);
};
} else {
return function (elem, evt, fn) {
var oldfn = elem['on' + evt];
elem['on' + evt] = function () {
if (oldfn && typeof oldfn === 'function') {
oldfn();
}
fn();
};
};
}
}(window));
var bind=(function(a){return a.addEventListener?function(a,b,c,d){a.addEventListener(b,c,d||false)}:a.attachEvent?function(a,b,c){a.attachEvent('on'+b,c)}:function(a,b,c){var
d=a['on'+b];a['on'+b]=function(){d&&typeof d==='function'&&d(),c()}}})(window)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment