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
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.