This should be an easy one :)
I want to add a click event listener only in case it hasn't been added earlier already, and prevent it from running twice. I thought of accomplishing this like so:
HTML
<a href="http://www.google.com"> Google is your friend </a>
<a href="http://www.Bing.com"> Bing's alright as well </a>
JS
var links = document.getElementsByTagName("a");
for (var i=0; i<links.length; i++)
{
if (*Default has been prevented for links[i]*){ //how do I implement this check?
return; //do nothing, we're all done.
} else {
links[i].addEventListener("click",function(event){
event.preventDefault();
alert("Showing you this instead of opening this link!");
});
}
}
In my real world case the added event listener is preventing the default behavior. So how can I check if it has been prevented before? I want to make sure the listener won't be added twice. How can I implement this check?
Many thanks for any ideas.