4

Let's say I have the following link:

<a href="#" onclick="alert('You clicked a link.');">Click Me!</a>

When clicked this link will alert a message as well as appending a pound sign on the end of the page url.
This doesn't look very pretty is there any way to avoid it besides using javascript in the url itself:

<a href="javascript:alert('You clicked a link.');">Click Me!</a>
Web_Designer
  • 72,308
  • 93
  • 206
  • 262

2 Answers2

6

You have to prevent the default response from occurring.

The old-fashioned approach is to return false from it:

<a href="#" onclick="alert('You clicked a link.'); return false;">Click Me!</a>

Or, better:

<a href="#" id="myLink">Click Me!</a>

<script type="text/javascript">
window.onload = function() {
   document.getElementById('myLink').onclick = function(event) {
      alert('You clicked a link.');
      return false;
   };
};
</script>

The best approach nowadays is to call the proper method of the event property:

<a href="#" id="myLink">Click Me!</a>

<script type="text/javascript">
window.onload = function() {
   document.getElementById('myLink').onclick = function(event) {
      alert('You clicked a link.');
      event.preventDefault(); // <---
   };
};
</script>

It's also best to replace that # with an URI to some proper page, for people not using JavaScript. In some jurisdictions, accessibility is in fact a legal requirement.

Edit Fixed for bleedin' IE:

function f() {
   document.getElementById('myLink').onclick = function(e) {
      alert('You clicked a link.');

    if (!e) {
       var e = window.event;
    }

    // e.cancelBubble is supported by IE - this will kill the bubbling process.
    e.cancelBubble = true;
    e.returnValue  = false;

    // e.stopPropagation works only in Firefox.
    if (e.stopPropagation) {
        e.stopPropagation();
        e.preventDefault();
    }
};

window.onload = f;
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
4

The trick is return false on the event handler.

<a href="" onclick="alert('You clicked a link.'); return false">Click Me!</a>
bradley.ayers
  • 37,165
  • 14
  • 93
  • 99
  • 3
    You must give a non-empty href to the link, because IE6,7 wouldn't recognize it as such, so # would be ok as a href, as long as you keep return false; in the onclick. – bisko Apr 10 '11 at 21:55
  • @bisko Wow.. really? Even though an empty string is a valid URI? – bradley.ayers Apr 10 '11 at 21:58
  • 3
    Even better, give a real value for href - I.e. Something that actually returns a page if JavaScript is disabled or otherwise not working. – Bobby Jack Apr 10 '11 at 21:59
  • @bradley yes. It's a secret that IE6,7 complies fully with the standarts :) Yes, IE6,7 have this problem. I'm not sure for 8 and above. When you don't specify a href, the A behaves just a normal span :) – bisko Apr 10 '11 at 22:00
  • @Tom See http://labs.apache.org/webarch/uri/rfc/rfc3986.html#same-document and http://labs.apache.org/webarch/uri/rfc/rfc3986.html#path (the last link gives ABNF if the first is ambiguous) – bradley.ayers Apr 10 '11 at 22:04
  • I just tested `href=""` in IE6 and it works fine, perhaps you're confusing that with omitting the href attribute completely? (which does indeed make the element no longer a hyperlink) – bradley.ayers Apr 10 '11 at 22:08