eventie.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*!
  2. * eventie v1.0.6
  3. * event binding helper
  4. * eventie.bind( elem, 'click', myFn )
  5. * eventie.unbind( elem, 'click', myFn )
  6. * MIT license
  7. */
  8. /*jshint browser: true, undef: true, unused: true */
  9. /*global define: false, module: false */
  10. ( function( window ) {
  11. 'use strict';
  12. var docElem = document.documentElement;
  13. var bind = function() {};
  14. function getIEEvent( obj ) {
  15. var event = window.event;
  16. // add event.target
  17. event.target = event.target || event.srcElement || obj;
  18. return event;
  19. }
  20. if ( docElem.addEventListener ) {
  21. bind = function( obj, type, fn ) {
  22. obj.addEventListener( type, fn, false );
  23. };
  24. } else if ( docElem.attachEvent ) {
  25. bind = function( obj, type, fn ) {
  26. obj[ type + fn ] = fn.handleEvent ?
  27. function() {
  28. var event = getIEEvent( obj );
  29. fn.handleEvent.call( fn, event );
  30. } :
  31. function() {
  32. var event = getIEEvent( obj );
  33. fn.call( obj, event );
  34. };
  35. obj.attachEvent( "on" + type, obj[ type + fn ] );
  36. };
  37. }
  38. var unbind = function() {};
  39. if ( docElem.removeEventListener ) {
  40. unbind = function( obj, type, fn ) {
  41. obj.removeEventListener( type, fn, false );
  42. };
  43. } else if ( docElem.detachEvent ) {
  44. unbind = function( obj, type, fn ) {
  45. obj.detachEvent( "on" + type, obj[ type + fn ] );
  46. try {
  47. delete obj[ type + fn ];
  48. } catch ( err ) {
  49. // can't delete window object properties
  50. obj[ type + fn ] = undefined;
  51. }
  52. };
  53. }
  54. var eventie = {
  55. bind: bind,
  56. unbind: unbind
  57. };
  58. // ----- module definition ----- //
  59. if ( typeof define === 'function' && define.amd ) {
  60. // AMD
  61. define( eventie );
  62. } else if ( typeof exports === 'object' ) {
  63. // CommonJS
  64. module.exports = eventie;
  65. } else {
  66. // browser global
  67. window.eventie = eventie;
  68. }
  69. })( window );