83 lines
1.7 KiB
JavaScript
83 lines
1.7 KiB
JavaScript
/*!
|
|
* eventie v1.0.6
|
|
* event binding helper
|
|
* eventie.bind( elem, 'click', myFn )
|
|
* eventie.unbind( elem, 'click', myFn )
|
|
* MIT license
|
|
*/
|
|
|
|
/*jshint browser: true, undef: true, unused: true */
|
|
/*global define: false, module: false */
|
|
|
|
( function( window ) {
|
|
|
|
'use strict';
|
|
|
|
var docElem = document.documentElement;
|
|
|
|
var bind = function() {};
|
|
|
|
function getIEEvent( obj ) {
|
|
var event = window.event;
|
|
// add event.target
|
|
event.target = event.target || event.srcElement || obj;
|
|
return event;
|
|
}
|
|
|
|
if ( docElem.addEventListener ) {
|
|
bind = function( obj, type, fn ) {
|
|
obj.addEventListener( type, fn, false );
|
|
};
|
|
} else if ( docElem.attachEvent ) {
|
|
bind = function( obj, type, fn ) {
|
|
obj[ type + fn ] = fn.handleEvent ?
|
|
function() {
|
|
var event = getIEEvent( obj );
|
|
fn.handleEvent.call( fn, event );
|
|
} :
|
|
function() {
|
|
var event = getIEEvent( obj );
|
|
fn.call( obj, event );
|
|
};
|
|
obj.attachEvent( "on" + type, obj[ type + fn ] );
|
|
};
|
|
}
|
|
|
|
var unbind = function() {};
|
|
|
|
if ( docElem.removeEventListener ) {
|
|
unbind = function( obj, type, fn ) {
|
|
obj.removeEventListener( type, fn, false );
|
|
};
|
|
} else if ( docElem.detachEvent ) {
|
|
unbind = function( obj, type, fn ) {
|
|
obj.detachEvent( "on" + type, obj[ type + fn ] );
|
|
try {
|
|
delete obj[ type + fn ];
|
|
} catch ( err ) {
|
|
// can't delete window object properties
|
|
obj[ type + fn ] = undefined;
|
|
}
|
|
};
|
|
}
|
|
|
|
var eventie = {
|
|
bind: bind,
|
|
unbind: unbind
|
|
};
|
|
|
|
// ----- module definition ----- //
|
|
|
|
if ( typeof define === 'function' && define.amd ) {
|
|
// AMD
|
|
define( eventie );
|
|
} else if ( typeof exports === 'object' ) {
|
|
// CommonJS
|
|
module.exports = eventie;
|
|
} else {
|
|
// browser global
|
|
window.eventie = eventie;
|
|
}
|
|
|
|
})( window );
|