How do I cancel the link action?
Posted
by Tobbe
on Stack Overflow
See other posts from Stack Overflow
or by Tobbe
Published on 2010-04-21T20:10:56Z
Indexed on
2010/04/21
20:13 UTC
Read the original article
Hit count: 397
JavaScript
|javascript-events
I though returning false would be enough to cancel the link action, but apparently it isn't. When I click the link in the code below I do get the 'click' message, which is expected. But I also end up at 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 make the url NOT change when clicking the link? Pure JavaScript solutions only please, no additional libraries (like jQuery). It only has to work in FireFox 3+ and Chrome.
© Stack Overflow or respective owner