Why does the first anchor element confirm as expected, but the others do not?
Example code hosted here
Code:
<html>
<head>
<script type="text/javascript">
function RUSure(e) {
alert("RUSure");
return e.window.confirm('Are you sure?');
}
function MakeSure(e, url) {
alert("MakeSure");
var sure = RUSure(e);
if (sure)
window.location.hostname = location.hostname + url;
return sure;
};
</script>
<title>Example</title>
</head>
<body>
<a href="/?ThisWorksFine" onclick="return confirm('Are you sure?')">This works</a>
<div><a href="#" onclick=" makesure(e, '/?ThisDoesntCallConfirm');">This doesn't</a></div>
<div><a href="#" onclick="return makesure(e, '/?ThisDoesntCallConfirm');">This doesn't</a></div>
<div><a href="/?ThisDoesntCallConfirm" onclick="return RUSure(e);">This doesn't</a></div>
<div><a href="/?ThisDoesntCallConfirm" onclick="RUSure(e);">This doesn't</a></div>
<!--The below didn't work either -->
<!--<script>
window.onload(function () {
var aTags = document.getElementsByTagName('a');
for (var i = 0; i < aTags.length; i++)
aTags[i].addEventListener('click', RUSure);
});
</script>-->
</body>
</html>