Created
April 24, 2012 12:31
-
-
Save grid23/2479261 to your computer and use it in GitHub Desktop.
addEventListener poly
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
| //addEventListener polyfill with e.preventDefault and e.stopPropagation for old IEs | |
| // DO NOT PUT THAT ONE ON GLOBAL SCOPE, just saying... :D | |
| var addEventListener = (function(){ | |
| if ( window.addEventListener ) | |
| return function(el, ev, fn, c){ | |
| return el.addEventListener(ev, fn, !!c) | |
| } | |
| return function(el, ev, fn){ | |
| return el.attachEvent('on'+ev, function(e){ | |
| var e = e || window.event | |
| e.target = e.target || e.srcElement | |
| e.relatedTarget = e.relatedTarget || e.fromElement || e.toElement | |
| e.isImmediatePropagationStopped = e.isImmediatePropagationStopped || false | |
| e.preventDefault = e.preventDefault || function(){ | |
| e.returnValue = false | |
| } | |
| e.stopPropagation = e.stopPropagation || function(){ | |
| e.cancelBubble = true | |
| } | |
| e.stopImmediatePropagation = e.stopImmediatePropagation || function(){ | |
| e.stopPropagation() | |
| e.isImmediatePropagationStopped = true | |
| } | |
| if ( !e.isImmediatePropagationStopped ) | |
| fn(e) | |
| }) | |
| } | |
| }()) | |
| /* | |
| addEventListener(element, 'click', function(e){ | |
| e.preventDefault() | |
| }) | |
| */ |
Author
remy > You got the eye ! ( Gist edited. )
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Typo, should read:
e = e || window.event