I am writing some JavaScript that what I essentially want to do is confirm when a user clicks a link that they do actually want to click it.
My code currently looks like this:
var Anchors = document.getElementsByTagName("a");
for (var i = 0; i < Anchors.length ; i++)
{
Anchors[i].addEventListener("click", function () { return confirm('Are you sure?'); }, false);
}
This code displays the confirm box as I would expect to see it, but then navigates to the link regardless of the button pressed in the confirm box.
I believe the problem is related to my usage of the addEventListener
(or a limitation of the implementation of it) because if I add manually write the following in a HTML file, the behaviour is exactly what I would expect:
<a href="http://www.google.com" onclick="return confirm('Are you sure?')">Google</a><br />
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…