Code Snippet to add an handler to an event listener
And below Code Snippet to remove the handler from the event listener
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
| function commonjsAddEvent( obj, type, fn ) { if (obj.addEventListener) { obj.addEventListener( type, fn, false ); } else if (obj.attachEvent) { obj[ "e" +type+fn] = fn; obj[type+fn] = function () { obj[ "e" +type+fn]( window.event ); } obj.attachEvent( "on" +type, obj[type+fn] ); } else { obj[ "on" +type] = obj[ "e" +type+fn]; } } |
function commonjsAddEvent( obj, type, fn ) { if (obj.addEventListener) { obj.addEventListener( type, fn, false ); } else if (obj.attachEvent) { obj["e"+type+fn] = fn; obj[type+fn] = function() { obj["e"+type+fn]( window.event ); } obj.attachEvent( "on"+type, obj[type+fn] ); } else { obj["on"+type] = obj["e"+type+fn]; } }
And below Code Snippet to remove the handler from the event listener
01
02
03
04
05
06
07
08
09
10
11
12
| function commonjsRemoveEvent( obj, type, fn ){ if (obj.removeEventListener){ obj.removeEventListener( type, fn, false ); } else if (obj.detachEvent){ obj.detachEvent( "on" +type, obj[type+fn]); obj[type+fn]= null ; obj[ "e" +type+fn]= null ; } else obj[ "on" +type] = null ; } |
function commonjsRemoveEvent( obj, type, fn ){ if(obj.removeEventListener){ obj.removeEventListener( type, fn, false ); } else if(obj.detachEvent){ obj.detachEvent("on"+type, obj[type+fn]); obj[type+fn]=null; obj["e"+type+fn]=null; } else obj["on"+type] = null; }