How to avoid dupliucate anchors XHTML?

68 views Asked by At

I did a page and did a check on it using a software called WebKing and it tells me I have duplicate anchors??

<ul >
            <li><a href="#toc1" title="About The Code">About The Code</a></li>
            <li><a href="#toc2" title="Blah Blah Test">Link 2 is boring</a></li>
            <li><a href="#toc3" title="3rd link test">3rd line in</a></li>
            <li><a href="#toc4" title="Contact Manny">Contact Manny</a></li>

            <h3><a name="toc1" id="toc1">About the code</a></h3>
            <h3><a name="toc2" id="toc2">Link 2 test</a></h3>
            <h3><a name="toc3" id="toc3">3rd test</a></h3>
            <h3><a name="toc4" id="toc4">Contact Manny</a></h3>
 </ul>

So what am I doing wrong?? Do I change the id to something else?

2

There are 2 answers

10
Loktar On

The name and id attribute share the same namespace so they need to be different.

http://www.w3.org/TR/html401/struct/links.html#h-12.2.3

If you are writing valid XHTML try not to use the name tag.

http://www.w3.org/TR/xhtml1/

Section 4.10. The elements with 'id' and 'name' attributes Note that in XHTML 1.0, the name attribute of these elements is formally deprecated, and will be removed in a subsequent version of XHTML.

Also you have some h3's that aren't inside li's but are inside of a ul.

1
Quentin On

This looks like it is just some bad heuristics in the analysis software you are using. There isn't anything technically wrong with that code.

That said, a modern approach (i.e. not pandering to Netscape 4) would be to say:

            <h3 id="toc1">About the code</h3>

… and I suspect this would also avoid triggering the aforementioned bad heuristic.

You should probably have better ids too. id="about" — URLs that inform readers where they go are generally better than URLs that don't so /mypage/#about beats /mypage/#toc1

On the subject of bad style, the title attribute is there to provide advisory information about an element. It should contain helpful extra information. Your example has it duplicating the main text of the links. At best this will just be some extra bytes to download. At worst, you can expect some screen reader users to have to listen to the destination of every link being repeated.