Internal links with name or id?

3

If I want to refer to a part within a page I use links with a pad ( # ); as for example: http://mi.pagina.com/#ancla , will jump inside the page to the part identified with the anchor #ancla .

There are two methods that work to create that anchor / internal link:

  • Using a a with the attribute name :

    <h2><a name="ancla"></a>Título de Sección</h2>
    
  • or using any element with a id :

    <h2 id="ancla">Título de Sección</h2>
    
  • Except in exceptional cases (I checked that browsers react differently in case of conflict), it seems that both work the same. Is there a difference between the two methods? Is one of the two ways better than the other?

        
    asked by Alvaro Montoro 05.08.2016 в 04:46
    source

    1 answer

    5

    Is there a difference between the two methods?

    The main difference in the theory is that the first method is "obsolete but conforming" in HTML5, in other words it can continue to be used although it should not, according to W3: Obsolete but conforming features the id attribute should be used:

      

    Authors should not specify the name attribute on a elements. If the attribute is present, its value must not be the empty string and must be equal to the value of any of the IDs in the element's home subtree other than the element's own ID, if any, nor be equal to the value of any of the other attributes on elements in the element's home subtree. If this attribute is present and the element has an ID, then the attribute must be equal to the element ID. In earlier versions of the language, this attribute was intended as a way to specify possible targets for fragment identifiers in URLs. The id attribute should be used instead.

    The difference in practice should be , according to the specification, that the browser will first look for an element with a id equal to the one specified in the anchor ( #ancla ), which is' case sensitive, if you can not find it then it will look for an element with the name attribute.

    The explanation of this second point is found here: Navigating to a fragment: The indicated part of the document

      

    1 Apply the URL parser algorithm to the URL, and let fragid be the fragment component of the resulting URL record.

         

    ...

         

    7 No decoded fragid: If there is an element in the DOM that has a value attribute whose value is exactly equal to fragment (not decoded fragment), then the first such element in the order is the indicated part of the document ; stop the algorithm here.

    Is one of the two ways better than the other?

    Based on the theory, that is, the HTML5 documentation that we saw in the previous point, the best would be the second method, since the first method is not valid and may not be supported by browsers that follow the specification of form. strict.

        
    answered by 05.08.2016 / 06:00
    source