Created
June 8, 2010 19:05
-
-
Save livingston/430487 to your computer and use it in GitHub Desktop.
Cross-browser Compatible Bind
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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