Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there two ways to jump to a fragment identifier in HTML?

I always thought the standard way to specify a fragment identifier is by <a name="foo"></a>.

<a href="#foo">go to foo</a>

<a name="foo"></a>                        <!-- obsolete method, it seems -->
<p>some content under that anchor with name</p>

But it seems like this is the old way, and the new way is using an id, like this:

<a href="#bar">go to bar</a>

<p id="bar">some content under that p with id</p>

In fact, the W3C validator says that name is obsolete for the <a> element. So are there 2 ways to jump to the fragment identifier but 1 of them is obsolete? (And when did that happen?)

(there are other questions about the difference between id and name, but this one is about fragment identifier)

like image 874
nonopolarity Avatar asked Jan 27 '16 08:01

nonopolarity


3 Answers

So are there 2 ways to jump to the fragment identifier but 1 of them is obsolete?

There are two ways to identify a fragment.

(There are also two ways to jump to one, since you can do it with a URL or write a pile of JavaScript to scroll the page).

And when did that happen?

id was introduced in 1996 when HTML 4 came out. It effectively obsoleted the name attribute for anchors.

name was made officially obsolete in HTML 5 in 2014 (or in Living HTML on some date that I'm not going to try to figure out).

like image 95
Quentin Avatar answered Oct 20 '22 14:10

Quentin


Yes there are two ways to jump to a fragment identifier and both aren't obsolete ( except a element).

That's rules applied to all HTML 5 elements other than a (because in a hasn't name attribute in HTML5).

So shortly it's obsolete to idenfity name attribute as fragment idenitifier for a element as that's attribute depricated since HTML4.

Flow of accessing fragment from HTML5 Specification:

  • 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.
  • 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.
  • Otherwise, there is no indicated part of the document.
like image 41
Andriy Ivaneyko Avatar answered Oct 20 '22 14:10

Andriy Ivaneyko


Answer to your question: Yes, There are two ways to identify a fragment and one is obsolete.

What is Fragment Identifiers ?

  1. Fragment identifiers for text/plain.
  2. URIs refer to a location in the same resource. This kind of URI starts with "#" followed by an anchor identifier (called the fragment identifier).

Fragment Identifier using JS like below.

location.replace('#middle');
like image 37
Venkat.R Avatar answered Oct 20 '22 13:10

Venkat.R