How to remove duplicate from addEventListener
.
I have site which uses ajax to populate different divs when user makes certain calls. I also have a Javascript function which grabs all the links and make an addEventListener
so that I can filter the url and if the user clicked on certain links give an alert. Below is my Javascript function which creates the addEventListener
:
function supportFunction() {
var myFunction = function(e) {
var regExp = new RegExp('//'+location.hostname+'($|/)');
var href = this.href;
if (regExp.test(href)) {
e.preventDefault();
alert('same domain link');
}
}
var links = document.querySelectorAll('a');
for (var i = 0; i < links.length; i++) {
links[i].addEventListener('click', myFunction);
}
}
The problem I am facing is, as some of the content gets populated using ajax after the supportfunction()
is called, if any links are there in the newly populated content it wont be added to the addEventListener
. I tried calling the supportfunction whenever an ajax call is made but in that case duplicates are added to eventlistener. That is, if someone clicks on the same domain link they will get 2 alerts, and some case 3-4 alert depending upon how many time the supportfunction()
is called.
I guess I explained, what my problem is. Let me know if you want more explanation about the problem.
I tried googleing for an answer and found this link http://dean.edwards.name/weblog/2005/10/add-event/ but couldn't figure out how to use it in my case.
Note : I am looking for a pure Javascript solution not a jQuery solution. (I hope I am not demanding here :) )