-
-
Save jonathantneal/2415137 to your computer and use it in GitHub Desktop.
addEventListener for IE8
This file contains 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
!window.addEventListener && Element.prototype && (function (polyfill) { | |
// window.addEventListener, document.addEventListener, <>.addEventListener | |
// window.removeEventListener, document.removeEventListener, <>.removeEventListener | |
function Event() { [polyfill] } | |
Event.prototype.preventDefault = function () { | |
this.nativeEvent.returnValue = false; | |
}; | |
Event.prototype.stopPropagation = function () { | |
this.nativeEvent.cancelBubble = true; | |
}; | |
function addEventListener(type, listener, useCapture) { | |
useCapture = !!useCapture; | |
var cite = this; | |
cite.__eventListener = cite.__eventListener || {}; | |
cite.__eventListener[type] = cite.__eventListener[type] || [[],[]]; | |
if (!cite.__eventListener[type][0].length && !cite.__eventListener[type][1].length) { | |
cite.__eventListener['on' + type] = function (nativeEvent) { | |
var newEvent = new Event, newNodeList = [], node = nativeEvent.srcElement || cite, property; | |
for (property in nativeEvent) { | |
newEvent[property] = nativeEvent[property]; | |
} | |
newEvent.currentTarget = cite; | |
newEvent.pageX = nativeEvent.clientX + document.documentElement.scrollLeft; | |
newEvent.pageY = nativeEvent.clientY + document.documentElement.scrollTop; | |
newEvent.target = node; | |
newEvent.timeStamp = +new Date; | |
newEvent.nativeEvent = nativeEvent; | |
while (node) { | |
newNodeList.unshift(node); | |
node = node.parentNode; | |
} | |
for (var a, i = 0; (a = newNodeList[i]); ++i) { | |
if (a.__eventListener && a.__eventListener[type]) { | |
for (var aa, ii = 0; (aa = a.__eventListener[type][0][ii]); ++ii) { | |
aa.call(cite, newEvent); | |
} | |
} | |
} | |
newNodeList.reverse(); | |
for (var a, i = 0; (a = newNodeList[i]) && !nativeEvent.cancelBubble; ++i) { | |
if (a.__eventListener && a.__eventListener[type]) { | |
for (var aa, ii = 0; (aa = a.__eventListener[type][1][ii]) && !nativeEvent.cancelBubble; ++ii) { | |
aa.call(cite, newEvent); | |
} | |
} | |
} | |
nativeEvent.cancelBubble = true; | |
}; | |
cite.attachEvent('on' + type, cite.__eventListener['on' + type]); | |
} | |
cite.__eventListener[type][useCapture ? 0 : 1].push(listener); | |
} | |
function removeEventListener(type, listener, useCapture) { | |
useCapture = !!useCapture; | |
var cite = this, a; | |
cite.__eventListener = cite.__eventListener || {}; | |
cite.__eventListener[type] = cite.__eventListener[type] || [[],[]]; | |
a = cite.__eventListener[type][useCapture ? 0 : 1]; | |
for (eventIndex = a.length - 1, eventLength = -1; eventIndex > eventLength; --eventIndex) { | |
if (a[eventIndex] == listener) { | |
a.splice(eventIndex, 1)[0][1]; | |
} | |
} | |
if (!cite.__eventListener[type][0].length && !cite.__eventListener[type][1].length) { | |
cite.detachEvent('on' + type, cite.__eventListener['on' + type]); | |
} | |
} | |
window.constructor.prototype.addEventListener = document.constructor.prototype.addEventListener = Element.prototype.addEventListener = addEventListener; | |
window.constructor.prototype.removeEventListener = document.constructor.prototype.removeEventListener = Element.prototype.removeEventListener = removeEventListener; | |
})(); |
This file contains 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
http://jsfiddle.net/GuQaV/show/ |
Thank you! When testing in IE8, I get a type mismatch error on this part:
if (!cite.__eventListener[type][0].length && !cite.__eventListener[type][1].length) {
cite.detachEvent('on' + type, cite.__eventListener['on' + type]);
}
Thanks a ton for this rather complete implementation.
@timelf123, I saw this error when I had jquery included in the page.
how do?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This saved me a lot of frustration today. Thank you!