More than you ever wanted to know about anchors
An a
element has two purposes in HTML:
- It's an anchor, to which you can link;
- It can be a hyperlink to an anchor, a document, or any other URL.
If you write:
<a name="Chapter4">Chapter 4. The Counterpane</a>
... and post it on the web at a location like
http://example.com/dedicated/hawthorne.html
... then anyone can link to that specific anchor with this URL:
http://example.com/dedicated/hawthorne.html#Chapter4
The #
sign is reserved in URL's for just this purpose: to separate the anchor's name from the URL of the document containing the anchor.
As you've probably seen, the structure of URL's goes something (simplified a little!) like this:
(method) : // (host) / (path-to-document-on-host) # (anchor)
So, e.g.
http :// example.com / dedicated/hawthorne.html # Chapter4
method host path-to-document anchor
Fortunately, URL's are very lazy, and if you omit any part of them, they will default to taking that part from the current document. You can even omit parts of the path to the document, by creating relative URL's.
One type of relative URL is to leave off everything but the fragment. So, from within the same document, one might have a Table Of Contents with an entry like:
<li> <a href="#Chapter4">Chapter 4. The Counterpane</a> </li>
This allows the document to "travel" anywhere, because its host and path will default to the location wherever it happens to be.
Even better, since it's clear that the target anchor must be in the same document, (almost) every web browser (except some very esoteric and crotchety ones) would simply scroll (with more or less finesse) to the target anchor, without (re-)loading the document.
Now, since an a
might be an anchor, and might be a hyper-reference ("link"), you have to use one or the other attribute for the web browser to know which behaviour you intend.
Simply writing
<a> Foo. </a>
... is ambiguous. Should it be an anchor? Probably not; there's no name! Should it be an hyper-reference? Clearly not; there's no reference!
Now, it had been (and, in some browsers, may still be) that certain attributes of JavaScript'y goodness like onClick
were reserved for only very specific elements. So, in order to create a widget on the page upon which one might trap those events, one had to use an element that supported it.
It just so happens that hyper-reference (link) elements happen to support a lot of these events, even in very old and cranky web browsers like Netscape 4.0 who didn't like to talk about events on things like paragraphs and divisions.
So, it became rather common to create a link, and then hide the fact that it was a link, just in order to trap a JavaScript event like click
on it.
As mentioned before, URL's are very forgiving about your omitting of various parts of them; and also, a fragment within the same document written like
<a href="#some-anchor"> ... </a>
...also has the bonus property of not (re-)loading the document. So what happens if we strip it down to its barest form?
href="#"
Well, there you have it. You have a reference to an anchor in the current document with no name. Since that anchor can't possibly exist, all that happens is ... nothing.
(Well, often nothing. The downside is, the browser is meant to scroll to the top of the document, but since you're trying to trap their click
in JavaScript --- you are, aren't you? --- you'll presumably cancel the navigation event before that can happen.)
So, the reason for href="#"
links is:
- This very particular form of link will do nothing, and yet still qualify as a valid hyper-reference, so you can attach JavaScript event listeners to it, no matter how cantankerous of an ancient web browser your end-users manage to dig up
Another, similar gimmick is to use href="javascript:void(0);"
The main difference between href="#"
and this, is that if your user doesn't have JavaScript enabled, they'll see different behaviour.
href="#"
will do nothing; or, maybe scroll to the head of the document, which might leave them wondering what the link was supposed to do.
href="javascript:void(0);"
will usually pop up some kind of dialog box mocking them for disabling their JavaScript, annoying them, but probably making them fully aware that they're missing out on "something" at that point.
Which one you prefer is a matter of taste, I suppose, although one of them is much easier to type.
PS: the other disadvantage of href="javascript:void(0);"
(or similar) is that it can't be copied as a reference to the current document. If someone, for example, does a right-click → “copy link location,” “open in new tab,” or so forth (eg, middle-click to open in a new tab) on href="#"
, the browser will usually do a somewhat sane thing, and use the location of the current document. On the other hand, opening javascript:void(0)
in a new tab will probably leave someone staring at a blank window, scratching their head.