8

I do not intend to start a debate.

If we want to make use of the onClick event, we should, on a certain way to disable the href to trigger. - Is this correct ?

If the above is correct, I believe that javascript:void(0) has the advantage of NOT triggering the scroll behavior.

Are those assumptions correct?

Note: I do not intend to search for a chimera, but my quest is about finding a way to style buttons in a cross-browser way with no accessibility hit (at all), without hacks and quirks.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
MEM
  • 30,529
  • 42
  • 121
  • 191

4 Answers4

17

If you want to prevent following the link, you should add event.preventDefault() in your click handler (event.returnValue = false; in IE).

It seems that you are just after the look of a link and not its functionality (or purpose). If so, you can use a button with CSS to style it accordingly.

Having real links with href contents # or javascript:void(0) should be avoided.

Further explanation:

Using a link just to have something "clickable" is not good. A link has a distinct syntactical meaning. As you can assign a click event handler to every element, you can use any other element for that.

The syntactically most correct one (imo) would be button as you will still have the possibility to use tab to navigate on them. You can style them with CSS to make them look like a link if you want to (see this example).

Now regarding preventing the default action:

Assuming you have a normal link with a proper href value and you want to intercept the click. In the click handler you assign to the element, e.g.

link.addEventListener('click', function(event){
    // some code
    event.preventDefault();
}, false);

using event.preventDefault() prevents the default action, which in case of a link, is following the URL.

(the code above is an example for W3C compatible browsers, IE is a bit different)

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • Care to place a full example of the above? Where should event.preventDefault be placed? – MEM Jan 30 '11 at 12:57
  • @Christoph: Thanks, nice move of yours! – Felix Kling Jan 30 '11 at 12:58
  • True that I'm after the look over the functionality. I was not aware that we could place a onclick event on a span. :) On a semantic propose however, I would prefer to use a instead, even if, with that, I need to disable the default href event. Not clear to me, what could be an example of the above, that could be valid for IE and FF - could that be on one and same line? – MEM Jan 30 '11 at 13:05
  • About the Further explanation: Is button a cross-browser solution? – MEM Jan 30 '11 at 13:07
  • @MEM: Yes it is. It will render a button on the page, but clicking it won't do anything. You have to assign this functionality via JavaScript. – Felix Kling Jan 30 '11 at 13:09
  • @Felix Kling - You stated that we can style a button as if it was a link. 1) So, a simple text with an underline "on hover" can be done? 2) Could :active, :hover subclasses be applied to it on the css side ? (I do accept links that say "Go read" as an answer) ;) – MEM Jan 30 '11 at 13:17
  • @MEM: I just made an example: http://jsfiddle.net/9j5yb/1/ `:hover` should work in all browsers but IE6. `:active` can only be applied to links afaik. – Felix Kling Jan 30 '11 at 13:19
  • @MEM: I just saw that you can use `:active` at least in Chrome, but you only see the effect while you are clicking (leave the mouse button pressed to see it): http://jsfiddle.net/9j5yb/5/ – Felix Kling Jan 30 '11 at 13:21
  • Not sure if it is me, or jsfiddle but when I try to: .link:hover {text-decoration:underline;} I get no effect. (this is more a usability then acessibility issue, since most of the users are waiting to see a underline there. ... – MEM Jan 30 '11 at 13:24
  • @MEM: Which browser are you using? – Felix Kling Jan 30 '11 at 13:27
  • @MEM: I just tested with Firefox and it is not working for me either, which is weird because MDC's docs indicated that it should: https://developer.mozilla.org/En/CSS/:hover **update:** changing e.g. the color works, but not the underlining... strange. – Felix Kling Jan 30 '11 at 13:31
  • Opera, Chrome, all ok. I will check now IE and FF on M$ and Mac Safari and FF as well. Just to see if only a FF ubuntu related - can you see if the above is also the case, outside jsfiddle.net ? – MEM Jan 30 '11 at 13:32
  • @MEM: I tested in FF 3.6.13 on a Mac and both (jsfiddle and non-jsfiddle) have this problem. – Felix Kling Jan 30 '11 at 13:36
  • FF windows don't work either. So it's either a FF incompatibility versus button OR FF incompatibility on jsfiddle.net (so I wish). – MEM Jan 30 '11 at 13:37
  • @MEM: You can however simulate the underline by using `border-bottom` : http://jsfiddle.net/9j5yb/8/. And unfortunately, it is not a problem with jsfiddle. – Felix Kling Jan 30 '11 at 13:38
  • @Felix: Thanks a lot. :) This will be a permanent war isn't ? :-) Oh well... Time to measure the advantages and disadvantages, but your suggested solution clear has a LOT of advantages. :-) – MEM Jan 30 '11 at 13:39
  • @MEM: I'm glad I could help :) Web development seems to be always war ;) – Felix Kling Jan 30 '11 at 13:40
  • @Felix: "(the code above is an example for W3C compatible browsers, IE is a bit different)" - One last question: Where/how should we deal with that difference? I presume your example worked because you did make use of jquery framework that deals with (most of) cross browser inconsistencies ? – MEM Jan 30 '11 at 13:42
  • 1
    @MEM: You mean assigning the click handler? First: If you use a button, you don't need to prevent the default action, as there is none. Second: Still, assigning the click handler is different. Have a look at: http://www.quirksmode.org/js/events_advanced.html (another option is to use `link.onclick` as described here: http://www.quirksmode.org/js/events_tradmod.html). The advanced model has the advantage to assign *multiple* event handlers for a certain event to one element. With `onclick` you can only assign one handler but it might be sufficient for you. Just don't do this in the HTML. – Felix Kling Jan 30 '11 at 13:46
0

If you have to have an href, use javascript:void(0), because it has no effect, unlike #. But you can just have no href at all, give the a a class to make it look like a link, e.g.:

/* in stylesheet */
a.scriptlink {
    color: blue;
    text-decoration: underline;
    cursor: pointer
}

<!-- in HTML -->
<a class="scriptlink" onclick="whatever">
configurator
  • 40,828
  • 14
  • 81
  • 115
0

I prefer javascript:void(0) over # because it does not alter the address bar by putting a # there (which may affect any future scripts that use/alter the URL).

But better than both of them, I prefer to have the onclick function return false (so it doesn't navigate at all) and then put an URL that would result in the same action as clicking. For example, lets say clicking the link loads some content into a 'mainArea' div. Then the URL would reload the whole page, with that mainArea filled with the same thing. The advantage to doing this is that if they right-click the url and Copy Address or Open in New Tab, it still works.

<a href="/FullUrl" onclick="return doClick(this);" >Stuff</a>
Craig Celeste
  • 12,207
  • 10
  • 42
  • 49
  • Wondering why no one as commented Parched Squid suggestion... Even with the button the "open on new tab" is a nice option that, according to the case, the user should have. (paragraph) On those cases where a click will occur and the user stays on the same page, the full url should be the one that we are actually in? Is that it? – MEM Jan 30 '11 at 13:54
0

If you want to seperate markup and scripting, you might also consider using event delegation to set-up the event handlers.

Many Javascript frameworks (eg jQuery) come with cross-browser abstractions to make this less of a bother; because of NIH-syndrome, I rolled my own solution, though.

A complete example could look like this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
 "http://www.w3.org/TR/html4/strict.dtd">
<html>
 <head>
  <title>Test</title>
  <script type="text/javascript"
   src="http://mercurial.intuxication.org/hg/js-hacks/raw-file/tip/capture.js">
  </script>
  <script type="text/javascript">
// un-hide elements
document.documentElement.className = 'js';

// use event-delegation to capture click-events
capture('click', '.clicky', function(event) {
    alert(this.title);
    return false;
});
  </script>
  <style type="text/css">
/* display element only if scripting is enabled */
.clicky { display: none; }
.js .clicky { display: inline; }
  </style>
 </head>
 <body>
  <!-- link shouldn't be user-accessible; nevertheless, direct user to
   error page -->
  <div>
   <a href="no-js.html" class="clicky" title="clickety-click">click me</a>
  </div>
 </body>
</html>
Christoph
  • 164,997
  • 36
  • 182
  • 240