tags:

views:

11073

answers:

8

When I want to refer to some part of a webpage with the "http://example.com/#foo"-method, which one is The One to use:

<h1><a name="foo"/>Foo Title</h1>

or

<h1 id="foo">Foo Title</h1>

I know that both work, but are they equal, or do they have semantic differences?

Edit: The (X)HTML-dialect I'm working on is HTML5, but don't let that constrain your answers and feel free to answer dialect-agnostically.

A: 

The whole "named anchor" concept uses the name attribute, by definition. You should just stick to using the name, but the ID attribute might be handy for some javascript situations.

As in the comments, you could always use both to hedge your bets.

Alex Fort
I would use both too
Primetime
When using both, are the id:s and names globally unique? as in, can I use the same string as both the id and the name?
Henrik Paul
You can, but some people think it's bad practice.
Alex Fort
According to the HTML specification, if both are present, name and id should be identical. It also says that names and ids are in the same namespace. The HTML validator service doesn't check for these, and I doubt browsers care, but they seem like good guidelines to follow anyway.
erickson
+2  A: 

ID method will not work on older browsers, anchor name method will be deprecated in never HTML versions... I'd go with id.

Spikolynn
Do you have a source for those claims? Don't get me wrong; I'm just generally interested.
Henrik Paul
http://www.w3.org/TR/2008/WD-html5-diff-20080610/#absent-attributes
Spikolynn
+7  A: 

I have to say if you are going to be linking to that area in the page... such as page.html#foo and Foo Title isn't a link you should be using:

<h1 id="foo">Foo Title</h1>

If you instead put an <a> reference around it you're headline will be influenced by an <a> specific CSS within your site. It's just extra markup, and you shouldn't need it. It'd highly recommend to going with placing an id on the headline, not only is it better formed, but it will allow you to either address that object in Javascript or CSS.

Tim K.
+1  A: 

The second sample assigns a unique ID to the element in question. This element can then be manipulated or accessed using DHTML.

The first one, on the other hand, sets a named location within the document, akin to a bookmark. Attached to an "anchor", it makes perfect sense.

Cerebrus
+17  A: 

According to the HTML 5 specification, 5.9.8 Navigating to a fragment identifier:

For HTML documents (and the text/html MIME type), the following processing model must be followed to determine what the indicated part of the document is.

  1. Parse the URL, and let fragid be the component of the URL.
  2. If fragid is the empty string, then the indicated part of the document is the top of the document.
  3. If there is an element in the DOM that has an ID exactly equal to fragid, then the first such element in tree order is the indicated part of the document; stop the algorithm here.
  4. If there is an a element in the DOM that has a name attribute whose value is exactly equal to fragid, then the first such element in tree order is the indicated part of the document; stop the algorithm here.
  5. Otherwise, there is no indicated part of the document.

So, it will look for id="foo" followed by name="foo"

Edit: As pointed out by @hsivonen, in HTML5 the a element has no name attribute. However, the above rules still apply to other named elements.

Greg
There’s no implied relationship between that algorithm and validity. The <a name> is invalid in HTML5 as currently drafted.
hsivonen
Actually it does not apply to other "named elements". As far as the name attributes goes it just applies to <a name>. However, that attribute may not be used by authors. It just has to be honored by user agents for older HTML pages.
Anne
+1  A: 

There's no semantic difference; the trend in the standards is toward the use of id rather than name. However, there are differences that may lead one to prefer name in some cases. The HTML 4.01 specification offers the following hints:

Use id or name? Authors should consider the following issues when deciding whether to use id or name for an anchor name:

  • The id attribute can act as more than just an anchor name (e.g., style sheet selector, processing identifier, etc.).
  • Some older user agents don't support anchors created with the id attribute.
  • The name attribute allows richer anchor names (with entities).
erickson
To be clear, when they say "older user agents" they mean REALLY old user agents. I wouldn't worry about that.
Eli
+8  A: 

You shouldn’t use <h1><a name="foo"/>Foo Title</h1> in any flavor of HTML served as text/html, because the XML empty element syntax isn’t supported in text/html. However, <h1><a name="foo">Foo Title</a></h1> is OK in HTML4. It is not valid in HTML5 as currently drafted.

<h1 id="foo">Foo Title</h1> is OK in both HTML4 and HTML5. This won’t work in Netscape 4, but you’ll probably use a dozen other features that don’t work in Netscape 4.

hsivonen
A: 
<h1 id="foo">Foo Title</h1>

is what should be used. Don't use an anchor unless you want a link.

Andrew Marsh