How to undo a link?

I would return false to undo the link, but apparently it isn't. When I click the link in the code below, I get the 'click' message that is expected. But I also end up on google.com, which is unexpected.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
    <title>Link test</title>
    <script>
        window.addEventListener('load', function () {
            document.getElementsByTagName('a')[0].addEventListener('click', function () {
                alert('click');
                return false;
            }, false);
        }, false);
    </script>
</head>
<body>
    <a href="http://www.google.com">google</a>
</body>
</html>

      

How can I get the url NOT to change when the link is clicked? Pure JavaScript solutions, please no additional libraries (like jQuery). It should only work in FireFox 3+ and Chrome.

+2


a source to share


1 answer


If you don't want the anchor url to change location you can try the following



document.getElementsByTagName('a')[0].addEventListener('click', function (e) {
            alert('click');
            e=e¦¦event; 
            e.preventDefault? e.preventDefault() : e.returnValue = false; 
        }, false);

      

+1


a source







All Articles