Last active
December 20, 2015 12:29
-
-
Save codeBelt/6132015 to your computer and use it in GitHub Desktop.
addEventListener and removeEventListener scope polyfill. Not fully tested in all browsers.
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title></title> | |
<script src="polyfill.addEventListener.js"></script> | |
<script> | |
window.onload = function() { | |
(function() { | |
var Test = function() { | |
this.link = document.getElementById('js-link'); | |
this.link.addEventListener('click', this.test, this, false); | |
this.image = document.getElementById('js-image'); | |
this.image.addEventListener('click', this.test, this, false); | |
}; | |
Test.prototype.test = function(event) { | |
event.preventDefault(); | |
alert('click') | |
event.target.removeEventListener('click', this.test, this); | |
}; | |
var test = new Test(); | |
})(); | |
} | |
</script> | |
</head> | |
<body> | |
<a href="javascript:void(0);" id="js-link">click</a> | |
<img id="js-image" width="100px" src="http://www.capca.ca/wp-content/uploads/Canada-small-87393222.jpg"/> | |
</body> | |
</html> | |
/** | |
* Polyfill for fix scoping and reference issues. | |
*/ | |
//https://developer.mozilla.org/en-US/docs/Web/API/EventTarget.removeEventListener | |
(function(window, document) | |
{ | |
// if (!Element.prototype.addEventListener) { | |
var oListeners = {}; | |
function runListeners(event) | |
{ | |
if (!event) | |
{ | |
event = window.event; | |
} | |
for (var iLstId = 0, iElId = 0, oEvtListeners = oListeners[event.type]; iElId < oEvtListeners.elementList.length; iElId++) | |
{ | |
if (oEvtListeners.elementList[iElId] === this) | |
{ | |
for (iLstId; iLstId < oEvtListeners.elementCallbackList[iElId].length; iLstId++) | |
{ | |
var obj = oEvtListeners.elementCallbackList[iElId][iLstId]; | |
if (obj.e === this) { | |
obj.c.call(obj.s, event); | |
} | |
} | |
break; | |
} | |
} | |
} | |
Element.prototype.addEventListener = function(type, callback, scope, useCapture) | |
{ | |
if (oListeners.hasOwnProperty(type)) | |
{ | |
var oEvtListeners = oListeners[type]; | |
for (var nElIdx = -1, iElId = 0; iElId < oEvtListeners.elementList.length; iElId++) | |
{ | |
if (oEvtListeners.elementList[iElId] === this) | |
{ | |
nElIdx = iElId; | |
break; | |
} | |
} | |
if (nElIdx === -1) | |
{ | |
oEvtListeners.elementList.push(this); | |
oEvtListeners.elementCallbackList.push([ | |
{c: callback, s: scope, e: this} | |
]); | |
this["on" + type] = runListeners; | |
} | |
else | |
{ | |
var aElListeners = oEvtListeners.elementCallbackList[nElIdx]; | |
if (this["on" + type] !== runListeners) | |
{ | |
aElListeners.splice(0); | |
this["on" + type] = runListeners; | |
} | |
for (var iLstId = 0; iLstId < aElListeners.length; iLstId++) | |
{ | |
if (aElListeners[iLstId].c === callback && aElListeners[iLstId].s === scope && aElListeners[iLstId].e === this) | |
{ | |
return; | |
} | |
} | |
aElListeners.push({c: callback, s: scope, e: this}); | |
} | |
} | |
else | |
{ | |
oListeners[type] = { elementList: [this], elementCallbackList: [ | |
[ | |
{c: callback, s: scope, e: this} | |
] | |
] }; | |
this["on" + type] = runListeners; | |
} | |
}; | |
Element.prototype.removeEventListener = function(type, callback, scope, useCapture) | |
{ | |
if (!oListeners.hasOwnProperty(type)) | |
{ | |
return; | |
} | |
var oEvtListeners = oListeners[type]; | |
for (var nElIdx = -1, iElId = 0; iElId < oEvtListeners.elementList.length; iElId++) | |
{ | |
if (oEvtListeners.elementList[iElId] === this) | |
{ | |
nElIdx = iElId; | |
break; | |
} | |
} | |
if (nElIdx === -1) | |
{ | |
return; | |
} | |
for (var iLstId = 0, aElListeners = oEvtListeners.elementCallbackList[nElIdx]; iLstId < aElListeners.length; iLstId++) | |
{ | |
if (aElListeners[iLstId].c === callback && aElListeners[iLstId].s === scope && aElListeners[iLstId].e === this) | |
{ | |
aElListeners.splice(iLstId, 1); | |
} | |
} | |
}; | |
// } | |
})(window, document); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment